Infolinks In Text Ads

Wednesday, 17 August 2011

Server Sockets and Listeners


 Server Sockets and Listeners
 We were able to contact the NIST server because it was already listening.  If we want to be able to set up a network connection entirely on our own, say between two students in this class, then both computers will have to create sockets, and one will have to be “listening”.   If  this is done at a low level,  using methods of the Socket class, it requires a basic knowledge of threads.  Here is the reason, which can be explained in language-independent terms.  What needs to happen is this:
  • We need to create a “server socket” and set it to “listen”.  It then waits until some client tries to connect.
  • Then,  the socket needs to “accept” the client,  and at that point the socket can be used to transmit and receive data.
While the socket is “listening”, however,  execution doesn’t continue.  It “hangs” at the Listen line of code and doesn’t go beyond until a connection is made.   So,  unless we want our whole program to be unusable after you press the Listen button until someone connects,  we have to create a separate “thread” of execution to handle the Listen command.
Since this is a common task in network programming,  .NET has provided a special class to handle this task, that is usable without a knowledge of the System.Threading library.    Specifically, there is a .NET class TcpListener.  We will use that class in the next example.
Inter-Process Communication i.e. the capability of two or more physically connected machines to exchange data, plays a very important role in enterprise software development. TCP/IP is the most common standard adopted for such communication. Under TCP/IP each machine is identified by a unique 4 byte integer referred to as its IP address (usually formatted as 192.168.0.101). For easy remembrance, this IP address is mostly bound to a user-friendly host name. The program below (showip.cs) uses the System.Net.Dns class to display the IP address of the machine whose name is passed in the first command-line argument. In the absence of command-line arguments, it displays the name and IP address of the local machine.
using System;
using System.Net;
class ShowIP{
    public static void Main(string[] args){
        string name = (args.Length < 1) ? Dns.GetHostName() : args[0];
        try{
            IPAddress[] addrs = Dns.Resolve(name).AddressList;
            foreach(IPAddress addr in addrs)
                Console.WriteLine("{0}/{1}",name,addr);
        }catch(Exception e){
            Console.WriteLine(e.Message);
        }
    }
}
Dns.GetHostName() returns the name of the local machine and Dns.Resolve() returns IPHostEntry for a machine with a given name, the AddressList property of which returns the IPAdresses of the machine. TheResolve method will cause an exception if the mentioned host is not found.
Though IPAddress allows to identify machines in the network, each machine may host multiple applications which use network for data exchange. Under TCP/IP, each network oriented application binds itself to a unique 2 byte integer referred to as its port-number which identifies this application on the machine it is executing. The data transfer takes place in the form of byte bundles called IP Packets or Datagrams. The size of each datagram is 64 KByte and it contains the data to be transferred, the actual size of the data, IP addresses and port-numbers of sender and the prospective receiver. Once a datagram is placed on a network by a machine, it will be received physically by all the other machines but will be accepted only by that machine whose IP address matches with the receiver�s IP address in the packet. Later on, this machine will transfer the packet to an application running on it which is bound to the receiver�s port-number present in the packet.
TCP/IP suite actually offers two different protocols for data exchange. The Transmission Control Protocol (TCP) is a reliable connection oriented protocol while the User Datagram Protocol (UDP) is not very reliable (but fast) connectionless protocol.

Twitter Delicious Facebook Digg Stumbleupon Favorites More