Socket Programming in C#



The System.Net.Sockets namespace has a managed implementation of the Windows Sockets interface.

It has two basic modes − synchronous and asynchronous.

Let us see an example to work with System.Net.Sockets.TcpListener class −

TcpListener l = new TcpListener(1234);
l.Start();

// creating a socket
Socket s = l.AcceptSocket();
Stream network = new NetworkStream(s);

The following is the Socket useful in communicating on TCP/IP network −

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Above,

  • AddressFamily − It is the standard address families by the Socket class to resolve network addresses

  • SocketType − The type of socket

  • ProtocolType − This is the network protocol for communication on the Socket. It can be Tcp and Udp.

Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements