Friday, August 3, 2012

T-SQL script to find out which table is locked

This is a handy script and it is common in some scenarios that you get your tabled locked due to some deadlock condition and results in unavailability of such table in whole application. There are other ways to find out e.g: using SSMS can give you handful details of what you want but in case if you want some custom parameters with custom query you will have to understand which tables MS SQL server provides such information. There are tables in sys called dm_tran_locks that shows how many translations running in current system this tables gives us a lot of information but most of it is ids so to get descriptive values you will have to join these with other sys tables like 'objects', 'waiting_tasks' etc.. Hope this will help other as well.


SELECT t1.resource_type,
       t1.resource_database_id,
       t1.resource_associated_entity_id,
       t1.request_mode,
       t1.request_session_id,
       t2.blocking_session_id,
       o1.name         'object name',
       o1.type_desc    'object descr',
       p1.partition_id 'partition id',
       p1.rows         'partition/page rows',
       a1.type_desc    'index descr',
       a1.container_id 'index/page container_id'
FROM   sys.dm_tran_locks AS t1
       INNER JOIN sys.dm_os_waiting_tasks AS t2
               ON t1.lock_owner_address = t2.resource_address
       LEFT OUTER JOIN sys.objects o1
                    ON o1.object_id = t1.resource_associated_entity_id
       LEFT OUTER JOIN sys.partitions p1
                    ON p1.hobt_id = t1.resource_associated_entity_id
       LEFT OUTER JOIN sys.allocation_units a1
                    ON a1.allocation_unit_id = t1.resource_associated_entity_id 

Thursday, July 26, 2012

Easiest way to find how good is your software team

The Joel Test
  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?


The neat thing about The Joel Test is that it's easy to get a quick yes or no to each question. You don't have to figure out lines-of-code-per-day or average-bugs-per-inflection-point. Give your team 1 point for each "yes" answer. The bummer about The Joel Test is that you really shouldn't use it to make sure that your nuclear power plant software is safe.

A score of 12 is perfect, 11 is tolerable, but 10 or lower and you've got serious problems. The truth is that most software organizations are running with a score of 2 or 3, and they need serious help, because companies like Microsoft run at 12 full-time.

Of course, these are not the only factors that determine success or failure: in particular, if you have a great software team working on a product that nobody wants, well, people aren't going to want it. And it's possible to imagine a team of "gunslingers" that doesn't do any of this stuff that still manages to produce incredible software that changes the world. But, all else being equal, if you get these 12 things right, you'll have a disciplined team that can consistently deliver.

Thursday, July 19, 2012

T-SQL RANK,Row_Number,NTile and Dense_Rank

Rank():
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Usage: RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Dense_Rank() :
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
Usage: DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
Ntile():
Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.
Usage: NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > ) 
Row_Number():
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> ) 
Example 

All Recordset :  
select * from finals 
RecordID Name Surname Course Point
1 Bugra Postaci Mathematics 89
2 Bugra Postaci Lecture 90
3 Dany Lowe Mathematics 75
4 Dany Lowe Lecture 85
5 Alice Marcel Mathematics 77
6 Alice Marcel Lecture 100
7 Simon Duru Mathematics 45
8 Simon Duru Lecture 58

select row_number() over(order by RecordID desc) as RowNumber ,* from finals 

RowNumber RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 7 Simon Duru Mathematics 45
3 6 Alice Marcel Lecture 100
4 5 Alice Marcel Mathematics 77
5 4 Dany Lowe Lecture 85
6 3 Dany Lowe Mathematics 75
7 2 Bugra Postaci Lecture 90
8 1 Bugra Postaci Mathematics 89

select ntile(2) over(order by RecordID desc) as [Ntile] ,* from finals 

Ntile RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
1 7 Simon Duru Mathematics 45
1 6 Alice Marcel Lecture 100
1 5 Alice Marcel Mathematics 77
2 4 Dany Lowe Lecture 85
2 3 Dany Lowe Mathematics 75
2 2 Bugra Postaci Lecture 90
2 1 Bugra Postaci Mathematics 89

as you see ntile function just divide the scope with given number 
select rank() over (order by Surname) as [Rank], * from finals 
Rank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
3 3 Dany Lowe Mathematics 75
3 4 Dany Lowe Lecture 85
5 5 Alice Marcel Mathematics 77
5 6 Alice Marcel Lecture 100
7 1 Bugra Postaci Mathematics 89
7 2 Bugra Postaci Lecture 90

rank function is working like  match one ”Duru” as 1 match another “Duru” as 1 but total count as 2 , next surname matches “Lowe” as count +1 as 3 , another “Lowe” as 3  ; now total count is 4 matching next surname as “Marcel” is count + 1 as 5 and goes on …. 

select dense_rank() over (order by Surname) as [DenseRank], * from finals 

DenseRank RecordID Name Surname Course Point
1 7 Simon Duru Mathematics 45
1 8 Simon Duru Lecture 58
2 3 Dany Lowe Mathematics 75
2 4 Dany Lowe Lecture 85
3 5 Alice Marcel Mathematics 77
3 6 Alice Marcel Lecture 100
4 1 Bugra Postaci Mathematics 89
4 2 Bugra Postaci Lecture 90
 Dense rank its obvious like shown. 

if you want to make your denserank number reseting by groups there is a way use PARTITION BY
below example reseting ranks by using “partition by” by course column
select
dense_rank() over (Partition by course order by Surname) as [DenseRank], * from finals 
 

DenseRank RecordID Name Surname Course Point
1 8 Simon Duru Lecture 58
2 4 Dany Lowe Lecture 85
3 6 Alice Marcel Lecture 100
4 2 Bugra Postaci Lecture 90
1 7 Simon Duru Mathematics 45
2 3 Dany Lowe Mathematics 75
3 5 Alice Marcel Mathematics 77
4 1 Bugra Postaci Mathematics 89
Thats all folks…


Original Article: http://blog.bugrapostaci.com/2010/01/26/t-sql-rank-dense_rank-ntile-row_number/

Friday, July 6, 2012

Reading content of a locked File C# .Net

Sometimes it might get handy to read some file that may remain in use for very long time by other processes, one possible example of such process involves log files they are mostly locked by their parent processes and sometimes we need to read the content of file without stopping the main process. There could be two possible ways to read an locked file(locked means some other process have exclusive access to that file)

1. Create a Copy of that file using Snapshot copy of volume of course this option is not the optimal solution in case of large files and even snapshot copy is an easy task that can be managed through .net code easily.


2. Other option that i came across and works for me is by opening file in read/shared mode. You can use this method specially with log4net which don't acquire lock on file for a very long time.

FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);

while (!logFileReader.EndOfStream)
{
    string line = logFileReader.ReadLine();
    Console.WriteLine(line);
}

// Clean up
logFileReader.Close();
logFileStream.Close();
 
 

Creating And Setting Custom Principal

Authenticating a user internaly sets SecurityPrincipal Object at Thread level this patteren is used almost in every .net application e.g Web Application/Web Services/ Desktop applications and even Console Applications. What i needed was to set a custom defined object as security principal so that i can access additional parameters from it. This is what i came accross :

This is how i can get/set principal object using a Factory classs.

PrincipalFactory pf = new PrincipalFactory();
            pf.SetCurrentUser(new User() {UserID=1,Name="asasd" });
            var user = pf.GetCurrentUser();



Factory Class Code:

 public class PrincipalFactory
    {
        public MyCustomPrincipal GetCurrent()
        {
            return Thread.CurrentPrincipal as MyCustomPrincipal;
        }

        public User GetCurrentUser()
        {
            var principal = GetCurrent();
            return (principal != null ? principal.User : null);
        }

        public void SetCurrentUser(User user)
        {
            Thread.CurrentPrincipal = new MyCustomPrincipal(user);
        }
    }




My Custom Object that i want to be saved in Principle

 public class User
    {
        public int UserID { get; set; }
        public string Name { get; set; }
    }


Finally this is my Custom Principal that implements IPrincipal interface
 public class MyCustomPrincipal:IPrincipal
    {
        public MyCustomPrincipal(User user)
        {
            this.User = user;
        }
        public User User { get; set; }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get { return WindowsIdentity.GetCurrent(); }
        }

        public bool IsInRole(string role)
        {
            return true;
        }

        #endregion
    }


Thursday, June 28, 2012

Code formatter for blog posting

After some time searching for a good code formatter i came accross this http://www.manoli.net/csharpformat/. hope this will help other as well.

OData Service Custom Security

I need to create an Odata service POC that allows to return result only to authenticated users. I google about authentication with OData and found that there are many options like

1.Windows Auth
2.Forms Auth
3.Claim based Auth
4.Anonymous Auth

what i needed is much simple and easy that is somehome related to claim based auth. i have configured a QueryInterceptor on VwUsers to check token parameter and if found return the result otherwise return error. i have used Query String to get the token you can experiment this with http headers as well.


In web.config you have to set

authentication mode="off" in authentication element

This will be the service code:


 public class CareService : DataService<NorthwindEntities>

    {

        // This method is called only once to initialize service-wide policies.

        public static void InitializeService(DataServiceConfiguration config)

        {

            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

            // Examples:

            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

           

            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

            //Set a reasonable paging site

            //config.SetEntitySetPageSize("*", 25);

            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

        }

        [QueryInterceptor("VwUsers")]

        public Expression&gt; OnQueryVwUsersHeaders()
        {
            var request = HttpContext.Current.Request;
            if (!string.IsNullOrEmpty(request.QueryString["token"]))
            {

   

               //token verification here

                return (VwUser p) =&gt; true;

            }

            else

            {

                throw new DataServiceException(401,

                 "The user does not have permission to view Users");

                //return (VwUser p) =&gt; false;

            }

        }

    }

Tuesday, June 19, 2012

MS SQL sp_executesql problem with query plan

I have been using sp_executesql to run my query to take advantage of its query plan caching and better security that prevent injection attacks on web application. but after some time working on project i found that when row counts in a table exceeds million records and your query becomes so complex that even might contain 15-30 joins in such queries i found problems with sp_executelsql.

sp_executelsql is meant to re-use compiled query plans, so it doesn't re-sniff parameters when the same query again hits it so it ends up in using a plan which might be very slow(will tell you why a slower query plan) with the current parameter values.it appears to be that sp_executesql uses a different method for choosing indexes and apparently it is a broken method compared to a plain text query running in SSMS.

For Example:

What differs is the select query on a table contains many join under a sp_executesql is fed via a chain of nested loops while the text query is fed via a chain of hash matches. The construction of the views appears identical between the two version (which I would expect). Unfortunately, the remainder is very complex and they appear to be doing radically different things in the middle; even pulling an "actual" execution plan doesn't give actual execution times for the various sub components of the query. Really, I can't see any reason for sp_executesql to be choosing anything differently, but it reliably constructs a significantly slower plan.

Solution i found so far:
Parameter sniffing is a solution to this, so you could rename the parameters names or you might even swap the column names in where clause that cause sp_executesql to recreate a query plan instead of using an old slow plan, of course this is not the solution but it won't cache a slower plan for so long.

Thursday, June 14, 2012

How Reference Types are created and stored in .net

When a reference type is created (class , delegate, interface, string, or object), it's allocated onto the heap..Net has four different heaps: (gen0, gen1, gen2)(Small object Heap), and LOH (Large Object Heap). Everything that's 85k or smaller goes on one of the first three heaps, depending on creation time (moved from gen0 to gen1 to gen2, etc). Objects larger than 85k get placed on the LOH. The LOH is never compacted, so eventually, allocations of the type I'm doing will eventually cause an OOM error as objects get scattered about that memory space. These are known as managed heaps.
To create an object, all you need to do is use the new keyword; .NET will take care of creating, initializing and placing the object on the right heap, and reserving any extra memory necessary. After that you can pretty much forget about that object, because you don't have to delete it when you're finished with it.
when you create a reference type object using new keyword it is placed on heap and its reference is used mostly in current running stack. there are other possible sources where your object could be used as refrence:
  1. global/static object references
  2. CPU registers
  3. object finalization references (more later)
  4. Interop references (.NET objects passed to COM/API calls)
  5. stack references(mostly used here)
These 5 are actually GC root nodes from where object reference hierarchy is formed.Imagine the classic Customer class, which usually has a collection storing Order classes. When an Order is added to the order collection, the collection itself then holds a reference to the added order. If the instance of the Customer class had a stack reference to it as well.
this is how hierarchy is formed for a complex object, this is how GC see the references.
e:g. stack-reference for Customer object -> reference to List object of orders -> reference individual orders.
anything that loose reference from these 5 roots is prone to GC.
how memory is allocated to objects is bit complex and it usally grow by time as specified http://msdn.microsoft.com/en-us/magazine/cc163791.aspx
a simple example could be:
class MyClass { 
    string Test="Hello world Wazzup!"; 
    byte[] data=new byte[86000];  }
It's easy to assume that the size of MyClass when allocated includes:
• 19 characters
• 86,000 bytes.
In fact, the object's size will only include general class stuff, and the memory required to store the object pointers to the string and the byte array (class level variables), which are then separately allocated onto the heaps. The string will be allocated on the SOH, and its object reference held by the instance of the class; the byte array will be allocated onto the LOH, as it's bigger than 85 KB.
SOH can be compacted and there will be no fragmentation in it, while LOH can have fragmentation in memory.

Saturday, May 26, 2012

Chat Client using TCP Sockets

This is just a simple chat client to work with the Chat server discussed in earlier post. it is a winform application that contains two textboxes one for message input and other for historical broadcast messages.


 public partial class Form1 : Form
    {
        StringBuilder history;
        TcpClient client = new TcpClient();
        NetworkStream clientStream;

        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
            history = new StringBuilder();
            try
            {
                client.Connect(serverEndPoint);
                clientStream = client.GetStream();
                lblStatus.Text = "Connected to Server";
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
            catch { lblStatus.Text = "Unable to Connect to Server"; }
        }


        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);

                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                var msg = encoder.GetString(message, 0, bytesRead);
                System.Diagnostics.Debug.WriteLine(msg);
                if (string.IsNullOrWhiteSpace(msg))
                    SendMessage(msg);
                else
                {
                    history.AppendLine(msg);
                    tbHistory.SetPropertyThreadSafe(() => tbHistory.Text, history.ToString());
                }
            }

            tcpClient.Close();
        }


        private void btnBroadcast_Click(object sender, EventArgs e)
        {
            if (clientStream != null && client != null && client.Connected)
            {
                SendMessage(tbMessage.Text);
                tbMessage.Text = string.Empty;
            }
        }

        private void SendMessage(string msg)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(msg);
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (clientStream != null && client != null && client.Connected)
            {
                clientStream.Close();
                client.Close();
            }
            Application.Exit();

        }
    }

Multi-Threaded TCP Socket Chat Server

A multi-threaded chat server using c# .net. It accepts connections from the client and manage data reception in separate threads virtually unlimited clients can connect to this server depending on the machine capacity. This is just a server that will broadcast messages from the client to all other clients. also have some events to show server status and client count change.

Detecting client disconnect in abnormal situations like wire cut, improper shutdown,power failure are not supported by framework, we will have to periodically check for the client whether they are connected or not.





public class Server

    {

        //thread safe dictionary to store list of tcp client connection that will be used to broadcast

        ConcurrentDictionary AllClients { get; set; }
       

        #region Events
        public delegate void ClientCount(int count);
        private event ClientCount _ClientCountUpdated;
        public event ClientCount ClientCountUpdated
        {
            add { _ClientCountUpdated += value; }
            remove { _ClientCountUpdated -= value; }
        }

        private void RaiseCount(int count)
        {
            if (_ClientCountUpdated != null)
            {
               
                _ClientCountUpdated(count);
            }
        }


        public delegate void ServerStatus(string status);
        private event ServerStatus _StatusChanged;
        public event ServerStatus StatusChanged
        {
            add { _StatusChanged += value; }
            remove { _StatusChanged -= value; }
        }

        private void RaiseStatusChanged(string status)
        {
            if (_StatusChanged != null)
            {
                _StatusChanged(status);
            }
        }
        #endregion

        #region Properties
        private TcpListener tcpListener;
        private Thread listenThread;
        private int _TotalClientCount;
        private int TotalClientCount
        {
            get
            {
                lock (this)
                {
                    return _TotalClientCount;
                }
            }
            set
            {
                lock (this)
                {
                    _TotalClientCount = value;
                    RaiseCount(_TotalClientCount);
                }
            }
        }
        #endregion

        public Server()
        {
            AllClients = new ConcurrentDictionary();
            MonitorDisconnectedClients();
           
        }

        // this will monitor abnormal network disconnections
        private void MonitorDisconnectedClients()
        {
            new Thread(() =>
            {

                while (true)
                {
                    //an empty broadcast message similar to ping
                    BroadcastMessage(" ");
               
                    var clientsToRemove = AllClients.Values.Where(r => r.LastUpdate < DateTime.Now.AddSeconds(-5)).ToArray();
                    MyTcpClient temp = null;
                    foreach (var client in clientsToRemove)
                    {
                        client.Tcp.Close();
                        AllClients.TryRemove(client.GetHashCode().ToString(), out temp);
                    }
                    RaiseCount(AllClients.Count);
                    Thread.Sleep(5000);
                }
            }).Start();
        }

      

        public void Start()
        {
            RaiseStatusChanged("Starting...");
            this.tcpListener = new TcpListener(IPAddress.Any, 3000);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
            RaiseStatusChanged("Listening...");
        }

        //start to listen to tcp connections
        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
                var myClient = new MyTcpClient(client);
                //add client connection to to the thread safe dictionary
                AllClients.AddOrUpdate(myClient.GetHashCode().ToString(), myClient, (key, value) => { return value; });
                //create a thread to handle communication
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(myClient);
                RaiseCount(AllClients.Count);
            }
        }

        //this will handle messages comming from the client in a seperate thread
        private void HandleClientComm(object client)
        {
            MyTcpClient tcpClient = (MyTcpClient)client;
            NetworkStream clientStream = tcpClient.Tcp.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                   
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                   
                    AllClients.TryRemove(tcpClient.GetHashCode().ToString(), out tcpClient);
                    RaiseCount(AllClients.Count);
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                var msg = encoder.GetString(message, 0, bytesRead);
                System.Diagnostics.Debug.WriteLine(msg);
                tcpClient.LastUpdate = DateTime.Now;
                if(!string.IsNullOrWhiteSpace(msg))
                    BroadcastMessage(msg);
            }

            tcpClient.Tcp.Close();
        }

        // this is used to broadcast messages to all other clients, also in another thread
        private void BroadcastMessage(string msg)
        {
            new Thread(() => {
                foreach (var item in AllClients)
                {
                    var tcpClient = item.Value;
                    if (tcpClient.Tcp.Connected)
                    {
                        NetworkStream clientStream = tcpClient.Tcp.GetStream();
                        ASCIIEncoding encoder = new ASCIIEncoding();
                        byte[] buffer = encoder.GetBytes(msg);
                        clientStream.Write(buffer, 0, buffer.Length);
                        clientStream.Flush();
                    }
                }
            }).Start();
        }

      
        //used to close down all tcp connections
        public void Close()
        {
            foreach (var client in AllClients)
            {
                client.Value.Tcp.Close();
            }
        }
    }

//MyTcpClent is just a wrapper to hold updates of a tcp client connection

 public class MyTcpClient
    {
        public MyTcpClient(TcpClient client)
        {
            Tcp = client;
            LastUpdate = DateTime.Now;
        }
        public TcpClient Tcp { get; set; }
        public DateTime LastUpdate { get; set; }
    }

Friday, May 25, 2012

Public IP Address Finder in C#

This is a task i have been working on to find out the public ip of a computer running behind firewall or NAT.
It uses the trace route method using ping class and then filter out local IP address using regex and find the first available public IP and try to send it as an email, email part is not shown.

 public class IPFinder
    {
        internal void MyPublicIPAndSend()
        {
            Console.WriteLine("Wait finding Public IP...");
            var firstPublicIP = Traceroute("www.google.com").Where(r => !isIPLocal(r)).FirstOrDefault();
            if (firstPublicIP != null)
                SendEmail("My Public IP", firstPublicIP.ToString());

        }


        private bool isIPLocal(IPAddress ipaddress)
        {
            // find if ip is localhost
            // find if ip starts with 10.
            //find if ip falls in 172.1 to 172.31
            //find if ip is 192.168..
            var regEx = new Regex(@"(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)");
            return regEx.Match(ipaddress.ToString()).Success;
        }

        private IPAddress[] Traceroute(string ipAddressOrHostName)
        {
            List result = new List();
            //resolve name to ip
            IPAddress ipAddress = Dns.GetHostEntry(ipAddressOrHostName).AddressList[0];
            using (Ping pingSender = new Ping())
            {
                PingOptions pingOptions = new PingOptions();
                byte[] bytes = new byte[2];
                pingOptions.DontFragment = true;
                pingOptions.Ttl = 1;
                //30 hops just to safe guard infinite loop
                int maxHops = 30;
                for (int i = 1; i < maxHops + 1; i++)
                {
                    //send icmp echo to ipAddress with timout 5000 ms
                    PingReply pingReply = pingSender.Send(
                        ipAddress,
                        5000,
                        bytes, pingOptions);
                    result.Add(pingReply.Address == null ? string.Empty : pingReply.Address.ToString());
                    // keep looping until pingreply is success, means reached destination
                    if (pingReply.Status == IPStatus.Success)
                    {
                        break;
                    }
                    pingOptions.Ttl++;
                }
            }
            IPAddress temp = IPAddress.Parse("127.0.0.1");
            return result.Where(r => IPAddress.TryParse(r, out temp)).Select(r => IPAddress.Parse(r)).ToArray();
        }

        private bool SendEmail(string subject, string body)
        {
            Console.WriteLine(subject);
            Console.WriteLine(body);
            return true;

        }




    }

Network Interface details using .Net

I want to show All my PC's IP Addresses with their subnet masks. this .net don't have any property called subnet mask but it has IPv4Mask property that i used.


 NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Interface in Interfaces)
            {
                if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
                Console.WriteLine(Interface.Description);
                UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
                {
                    Console.WriteLine("\tIP Address is {0}", UnicatIPInfo.Address);
                    Console.WriteLine("\tSubnet Mask is {0}", UnicatIPInfo.IPv4Mask);
                }
            }

Trace Route using .Net Framwrok

I was working on a task where i need to work with trace route command if ICMP and in .net i was searching for any managed method of doing so, this is what i came across it seems like a simple and good approach towards trace route using .net

public string Traceroute(string ipAddressOrHostName)

{

    IPAddress ipAddress = Dns.GetHostEntry(ipAddressOrHostName).AddressList[0];

    StringBuilder traceResults = new StringBuilder();



    using(Ping pingSender = new Ping())

    {

        PingOptions pingOptions = new PingOptions();

        Stopwatch stopWatch = new Stopwatch();

        byte[] bytes = new byte[32];



        pingOptions.DontFragment = true;

        pingOptions.Ttl = 1;

        int maxHops = 30;



        traceResults.AppendLine(

            string.Format(

                "Tracing route to {0} over a maximum of {1} hops:",

                ipAddress,

                maxHops));


        traceResults.AppendLine();


        for(int i = 1; i < maxHops + 1; i++)

        {

            stopWatch.Reset();

            stopWatch.Start();

            PingReply pingReply = pingSender.Send(

                ipAddress,

                5000,

                new byte[32], pingOptions);



            stopWatch.Stop();


            traceResults.AppendLine(

                string.Format("{0}\t{1} ms\t{2}",

                i,

                stopWatch.ElapsedMilliseconds,

                pingReply.Address));


            if(pingReply.Status == IPStatus.Success)
            {
                traceResults.AppendLine();

                traceResults.AppendLine("Trace complete."); break;

            }


            pingOptions.Ttl++;

        }


    }


    return traceResults.ToString();
}

Wednesday, May 23, 2012

Tifflib Convert multipage Tiffs to a single stitched jpeg image

I was facing issues reading 8 bit tiff files using .net GDI+ as it do not support all Tiff formats so i opt to use tifflib(an open source library to manipulate tiff files) to read the tiff images and create a 16bit tiff image from it and then create a single stitched jpeg image. converting tiffs to jpeg is not ideal because jpeg will be very large in size but i have done this because in silverlight we have no support to display tiff natively. 

Hope this will help the others

 public bool convertTiffToJpg(string sourceFile, string targetFile)
        {

            bool response = false;

            try
            {

                // Get individual Images from the original image
                //Image sourceImage = Bitmap.FromFile(sourceFile,true);
                Image sourceImage = getBitmap8Bit(sourceFile);

                var total = sourceImage.GetFrameCount(FrameDimension.Page);
                var pageNumbers = Enumerable.Range(0, total).ToArray();
                Image[] sourceImages = new Image[pageNumbers.Length];
                for (int i = 0; i < pageNumbers.Length; i++)
                {
                    sourceImage.SelectActiveFrame(FrameDimension.Page, pageNumbers[i]);
                    float width = sourceImage.Width;
                    float height = sourceImage.Height;
                    ResizeImage(1024, sourceImage.Height, ref width, ref height);

                    using (var returnImage = new Bitmap(sourceImage, (int)width, (int)height))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            returnImage.Save(ms, ImageFormat.Jpeg);
                            sourceImages[i] = Image.FromStream(ms);
                        }
                    }
                }
                var p = new System.Collections.ArrayList();
                p.Add(Color.Black);
                p.Add(Color.Gray);
                p.Add(Color.LightGray);
                p.Add(Color.DarkGray);
                p.Add(Color.White);
                PaletteQuantizer quantizer = new PaletteQuantizer(p);
                //OctreeQuantizer quantizer = new OctreeQuantizer(2,2);
                //var quantizer = new WuQuantizer();
                // Merge individual Images into one Image
                var totalHeight = sourceImages.FirstOrDefault().Height * total;
                var totalWidth = sourceImages.FirstOrDefault().Width;
                using (var finalImage = new Bitmap(totalWidth, totalHeight))
                {

                    using (var g = Graphics.FromImage(finalImage))
                    {
                        g.InterpolationMode = InterpolationMode.Low;
                        g.PixelOffsetMode = PixelOffsetMode.HighSpeed;

                        // All other pages
                        for (int i = 0; i < pageNumbers.Length; i++)
                        {
                            g.DrawImage(sourceImages[i], new Point(0, sourceImages[i].Height * i));
                        }
                    }

                    //finalImage.MakeTransparent(Color.White);
                    using (var toout = quantizer.Quantize(finalImage))
                    {
                        ImageCodecInfo Codec = ImageCodecInfo.GetImageEncoders().Where(codec => codec.FormatID.Equals(ImageFormat.Png.Guid)).FirstOrDefault();
                        toout.Save(targetFile, Codec, GetCodedParams(null));
                    }

                }

                response = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return response;
        }

 private static Image getBitmap8Bit(string inputName)
        {

            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(1);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ImageCodecInfo info = GetEncoderInfo();
            foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
                if (ice.MimeType == "image/tiff")
                    info = ice;

            MemoryStream ms = new MemoryStream();
            Bitmap result = null;
            using (Tiff tif = Tiff.Open(inputName, "r"))
            {
                short dirs = tif.NumberOfDirectories();

                for (int page = 0; page < dirs; page++)
                {
                    if (tif.SetDirectory((short)page))
                    {
                        FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
                        int width = value[0].ToInt();

                        value = tif.GetField(TiffTag.IMAGELENGTH);
                        int height = value[0].ToInt();

                        int imageSize = height * width;
                        int[] raster = new int[imageSize];

                        if (tif.ReadRGBAImage(width, height, raster))
                        {
                            var bitmap = new Bitmap(width, height);
                            for (int i = 0; i < bitmap.Width; ++i)
                                for (int j = 0; j < bitmap.Height; ++j)
                                    bitmap.SetPixel(i, j, getSample(i, j, raster, width, height));
                            if (result == null)
                            {
                                result = bitmap;
                                result.Save(ms, info, ep);
                                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                            }
                            else
                            {
                                result.SaveAdd((bitmap as Image), ep);
                            }
                        }
                    }
                }
            }
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
            result.SaveAdd(ep);
            return Image.FromStream(ms);


        }

Sunday, May 20, 2012

ASP.Net MVC Views and Server Tags

ASP.Net MVC normal

<%   --- used to place .net code if else conditions etc --- %>
<%  Response.Write(str) %>  is equals to <%= str %>  // this is kind of a shortcut of Response.Write in MVC

now the problem is to precent plain string to be injected in response. in asp.net mvc3 we used <%= Html.Encode(str) %> to prevent script injection etc.. but in asp.net mvc 4 a new syntax is introduced as shortcut to Response.Write and Html.Encode, <%: str %>

ASP.Net MVC razor

@{ --- place if else conditions multiple lines etc ---}
@--- single line of code---   can also be used as shortcut to Response.Write @str
@{ Html.RenderPartial("view") }///Render Prefix methods will not return anything but void, they directly write to the response stream.
@Html.Partial /// returns htmlstring that can be used to store in some variable to print at some latter point eg:
@{var output = Html.Partial("View",model);} // assign html to variable
@output //send html to response

Thursday, May 10, 2012

Change Image Url in ASP.Net using HttpModule to point to some CDN

This is something i have created to change the image urls at runtime to point those url at some CDN server instead of my own server.
What i have done is simply intercept the a page if it ends with .aspx and them apply filter using my own created stream that will check the html markup and replace image url to CDN url.


ASPX Code:
 
 or even you can use 
 
 
HttpModule Code: 
  
public class CDNImageModule : IHttpModule
    {
        public void Dispose()
        {

        }
        public void Init(HttpApplication context)
        {
                context.BeginRequest += (o, e) =>
                {
                    if (context.Request.RawUrl.EndsWith(".aspx"))
                    {
                        var _watcher = new StreamWatcher(context.Response.Filter);
                        context.Response.Filter = _watcher;
                    }
                };
        }
    }

    public class StreamWatcher : Stream
    {

        public StreamWatcher(Stream sink)
        {
            _sink = sink;
        }

        private Stream _sink;

        #region Properites

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Flush()
        {
            _sink.Flush();
        }

        public override long Length
        {
            get { return 0; }
        }

        private long _position;
        public override long Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion

        #region Methods

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _sink.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _sink.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _sink.SetLength(value);
        }

        public override void Close()
        {
            _sink.Close();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] data = new byte[count];
            Buffer.BlockCopy(buffer, offset, data, 0, count);
            string html = System.Text.Encoding.Default.GetString(buffer);

            TransformString(ref html);

            byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
            _sink.Write(outdata, 0, outdata.GetLength(0));
        }

        #endregion


        void TransformString(ref string input)
        {

            string regExPatternForImgTags = "]* src=\\\"([^\\\"]*)\\\"[^>]*>"; //this will match all the  tags with src attribute, you may need to extend this. 

            MatchEvaluator evaluator = new MatchEvaluator(RewriteImageSrc);


            input = Regex.Replace(input, regExPatternForImgTags, evaluator);


        }

        public static string RewriteImageSrc(Match match)
        {
            return match.Value.Replace("src=\"", "src=\"http://img.mydomain.com/");

        }
}