An Introduction to Socket Programming in .NET using C#

Introduction

In this article, we will learn the basics of socket programming in .NET Framework using C#. Secondly, we will create a small application consisting of a server and a client, which will communicate using TCP and UDP protocols.

Pre-requisites

  • Must be familiar with .NET Framework.
  • Should have good knowledge of C#.
  • Basic knowledge of socket programming.

1.1 Networking basics:

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. The Resolve 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.

1.2 Client-Server programming with TCP/IP:

Under TCP there is a clear distinction between the server process and the client process. The server process starts on a well known port (which the clients are aware of) and listens for incoming connection requests. The client process starts on any port and issues a connection request.
The basic steps to create a TCP/IP server are as follows:
  1. Create a System.Net.Sockets.TcpListener with a given local port and start it:

    TcpListener listener = new TcpListener(local_port);
    listener.Start();
  2. Wait for the incoming connection request and accept a System.Net.Sockets.Socket object from the listener whenever the request appears:

    Socket soc = listener.AcceptSocket(); // blocks
  3. Create a System.Net.Sockets.NetworkStream from the above Socket:

    Stream s = new NetworkStream(soc);
  4. Communicate with the client using the predefined protocol (well established rules for data exchange):
  5. Close the Stream:

    s.Close();
  6. Close the Socket:

    s.Close();
  7. Go to Step 2.
Note when one request is accepted through step 2 no other request will be accepted until the code reaches step 7. (Requests will be placed in a queue or backlog.) In order to accept and service more than one client concurrently, steps 2 � 7 must be executed in multiple threads. Program below (emptcpserver.cs) is a multithreaded TCP/IP server which accepts employee name from its client and sends back the job of the employee. The client terminates the session by sending a blank line for the employee�s name. The employee data is retrieved from the application�s configuration file (an XML file in the directory of the application and whose name is the name of the application with a .config extension).

using System;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;

class EmployeeTCPServer{
    static TcpListener listener;
    const int LIMIT = 5; //5 concurrent clients
    
    public static void Main(){
        listener = new TcpListener(2055);
        listener.Start();
        #if LOG
            Console.WriteLine("Server mounted, 
                            listening to port 2055");
        #endif
        for(int i = 0;i < LIMIT;i++){
            Thread t = new Thread(new ThreadStart(Service));
            t.Start();
        }
    }
    public static void Service(){
        while(true){
            Socket soc = listener.AcceptSocket();
            //soc.SetSocketOption(SocketOptionLevel.Socket,
            //        SocketOptionName.ReceiveTimeout,10000);
            #if LOG
                Console.WriteLine("Connected: {0}", 
                                         soc.RemoteEndPoint);
            #endif
            try{
                Stream s = new NetworkStream(soc); 
                StreamReader sr = new StreamReader(s);
                StreamWriter sw = new StreamWriter(s);
                sw.AutoFlush = true; // enable automatic flushing
                sw.WriteLine("{0} Employees available", 
                      ConfigurationSettings.AppSettings.Count);
                while(true){
                    string name = sr.ReadLine();
                    if(name == "" || name == null) break;
                    string job = 
                        ConfigurationSettings.AppSettings[name];
                    if(job == null) job = "No such employee";
                    sw.WriteLine(job);
                }
                s.Close();
            }catch(Exception e){
                #if LOG
                    Console.WriteLine(e.Message);
                #endif
            }
            #if LOG
                Console.WriteLine("Disconnected: {0}", 
                                        soc.RemoteEndPoint);
            #endif
            soc.Close();
        }
    }
}
Here is the content of the configuration file (emptcpserver.exe.config) for the above application:



See full details: http://www.codeproject.com/KB/IP/socketsincsharp.aspx

Comments

Popular posts from this blog

Asynchronous Socket Programming in C#

Url Routing MVC TUTORIAL

WCF Chat Sample