Wednesday, December 11, 2013

Basic UDP socket echo client server program in C#


This program is same as previous chapter TCP Echo client server only difference is implementation of UDP socket. This chapter highlight the difference between TCP and UDP connection. TCP is analogous to telephone communication but UDP is analogous to mail box communication. TCP socket is required to establish a connection before data transfer where as UDP does not required to establish a connection before communication but we have to specify the destination address for each one i.e message called a datagram carries its own address information and is independent of all others. Message send through UDP can lost but in TCP doesn't. Data transfer rate is faster in UDP connection rather than TCP connection. So, UDP socket is used in such area where higher Data transfer rate is important rather than accurate data transfer for example live video telecast.

Download Source code

You can download the complete source code below:

Explaining the code

If you have read previous article TCP Echo client server you will understand code and find out differences with TCP. If you haven't read then read it because we will discuss only a added portion of code not the repeated portion.

Lets discuss the code.
Create UDP client instance and no need to specify server name/ IP and port number.
      // Create a UdpClient instance 

      UdpClient client = new UdpClient();

Send the message string to the server specifying destination address/server name and port number.
      // Send the echo string to the specified host and port

      client.Send(sendPacket, sendPacket.Length, server, servPort);
Create a remote IP end point for receiving. The IPEndPoint class specifies an address and port Compination. This IPEndPoint instance will be passed as a reference to the Receive() method,which will populate it with the remote sender's IP address and port information.
      // This IPEndPoint instance will be populated with the remote sender's

      // endpoint information after the Receive() call

      IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
Receive the message send by the server using Receive method. Receive method content the server name and port for the remote host that send the packet just received.
      // Attempt echo reply receive

      byte[] rcvPacket = client.Receive(ref remoteIPEndPoint); 

UDP Server
Server just receive client message and echoed back to the client.
       // Receive a byte array with echo datagram packet contents

        byte[] byteBuffer = client.Receive(ref remoteIPEndPoint);

        Console.Write("Handling client at " + remoteIPEndPoint + " - ");

        // Send an echo packet back to the client

        client.Send(byteBuffer, byteBuffer.Length, remoteIPEndPoint);

Running the program 

At first run the server at command line:
c:\Users\.......>UDPServer.exe     [press enter]

Now run the client in another command line:
C:\Users\.....>UDPClient.exe localhost "This is echo message"         [Press enter]


1 comment: