Socket Programming in C#

CsharpProgrammingServer Side Programming

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);

// 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);


  • 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.

Published on 21-Aug-2018 16:11:24