Wednesday, December 11, 2013

Basic .Net Socket Class Implementation in C#

Introduction

The .Net Framework provides a Socket class which has the all properties of TcpClient , TcpListener and UdpClient and plus its own methods. For details of .Net Socket class you can see here. In my previous articles I have shown you the basic implementation of TcpClient, TcpListener and UdpClient class. Here we are going to use the Socket class. Many things are same as previous articles so I like to discuss only Socket class implementation and ignore other things

Downloading the source code

Download complete source code from below:

Basic UDP socket echo client server program in C#

Introduction

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:
UDPClient
UDPServer 

Basic TCP socket echo client server program in C#

Introduction

Here we will create a simple echo client and echo server program where client send the message string to the server and server echoed back the same message string to the client. Download the both client and server source code compile it and run both in command window. IP address , port and message are passed through command line argument.

Download Source Code

You can download the complete source code form below.
TcpEchoClient
TcpEchoServer