Monday, June 18, 2012

Introduction to Socket Class in .Net

The .NET Framework provides a Socket class that is a wrapper around the WinSock implementation. Since TcpClient, TcpListener, and UdpClient all utilize the Socket class for their own implementations, Socket contains all the functionality of those classes, plus much more. The Socket interface is a generic API that actually covers more than just IP. Here we introduce its usage for TCP and UDP and walk through some common cases where you might use it.

TCP Client with Socket
For a TCP client to use the Socket class, it will perform the following steps:
1. Call the Socket constructor: The constructor specifies the address type, socket
type, and protocol type.
2. Call the Socket Connect() method: Connect() takes an IPEndPoint argument that
represents the server to connect to.
3. Send and receive data: Using the Socket Send() and Receive() calls.
4. Close the socket: Using the Socket Close() method.

Here we present a version of the TcpEchoClient.cs program that uses the Socket
class instead of the TcpClient class.
 using System; // For String, Int32, Console, ArgumentException
 using System.Text; // For Encoding
 using System.IO; // For IOException
 using System.Net.Sockets; // For Socket, SocketException
 using System.Net; // For IPAddress, IPEndPoint

 class TcpEchoClientSocket {

 static void Main(string[] args) {

 if ((args.Length < 2) || (args.Length > 3)) { // Test for correct # of args
 throw new ArgumentException("Parameters: []");

 String server = args[0]; // Server name or IP address
 // Convert input String to bytes
 byte[] byteBuffer = Encoding.ASCII.GetBytes(args[1]);

 // Use port argument if supplied, otherwise default to 7
 int servPort = (args.Length == 3) ? Int32.Parse(args[2]) : 7;

 Socket sock = null;

 try {
 // Create a TCP socket instance
 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,

 // Creates server IPEndPoint instance. We assume Resolve returns
 // at least one address
 IPEndPoint serverEndPoint = new IPEndPoint(Dns.Resolve(server).AddressList[0],
 // Connect the socket to server on specified port
 Console.WriteLine("Connected to server... sending echo string");

 // Send the encoded string to the server
 sock.Send(byteBuffer, 0, byteBuffer.Length, SocketFlags.None);

 Console.WriteLine("Sent {0} bytes to server...", byteBuffer.Length);

 int totalBytesRcvd = 0; // Total bytes received so far
 int bytesRcvd = 0; // Bytes received in last read

 // Receive the same string back from the server
 while (totalBytesRcvd < byteBuffer.Length) {
 if ((bytesRcvd = sock.Receive(byteBuffer, totalBytesRcvd,
 byteBuffer.Length - totalBytesRcvd, SocketFlags.None)) == 0) {
 Console.WriteLine("Connection closed prematurely.");
 totalBytesRcvd += bytesRcvd;

 Console.WriteLine("Received {0} bytes from server: {1}", totalBytesRcvd,
 Encoding.ASCII.GetString(byteBuffer, 0, totalBytesRcvd));

 } catch (Exception e) {
 } finally {

No comments:

Post a Comment