C# Multi threaded Server Socket programming

MultiThreaded Server Socket Program here is a C# Console based application , that can handle multiple clients at the same time. Network programming in windows is possible with sockets , peer-to-peer Microsoft Windows applications that act as servers and clients to send and receive data. You can see the basics of C# Socket Programming in the previous section , before you start this section take a look at C# Socket Programming .

csharp-multithreaded-socket-programming-1

The C# Multi Threaded Socket Program has two sections.

1. C# Multithreaded Server Socket Program

2. C# Multi Threaded Client Socket Program

Multithreaded Server Socket Program

Here we create a C# Server Socket from TcpListener Class and listen to PORT 8888 . When the C# Server Socket gets a request from Client side , the Server passes the instance of the client request to a separate class handleClient .For each call from Server Socket , the handleClient class create new instances for independent communication with the client. In handleClient class there is a Thread for handling the communication between the instance of C# Server side client and C# Client from outside .

For each Client request , there is a new thread instant is created in C# Server for separate communication with Client, so we can connect more than one client at the same time to the C# Server and communicate independently to Server .

Create a new C# Console Application project and put the following source code in the C# project.

Full Source C#

The C# Multi Threaded Socket Program has two sections.

1. C# Multithreaded Server Socket Program

2. C# Multi Threaded Client Socket Program

How to run this program ?

Create C# Multithreaded Server Socket Program and C# Multi Threaded Client Socket Program in two separate C# projects .

After compile and build the projects, open a DOS prompt and run the Server Program first.Then you will get the message "Server started" in Server side.

Next you start the Client program , then you can see the message from Server . You can start more than one client at the same time and communicate with the Server program. If you plan to run more than one client, it is better to run the client program's .exe file to copy in separate folders and run from .exe file.

Full Source C#
using System; using System.Threading; using System.Net.Sockets; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { TcpListener serverSocket = new TcpListener(8888); TcpClient clientSocket = default(TcpClient); int counter = 0; serverSocket.Start(); Console.WriteLine(" >> " + "Server Started"); counter = 0; while (true) { counter += 1; clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!"); handleClinet client = new handleClinet(); client.startClient(clientSocket, Convert.ToString(counter)); } clientSocket.Close(); serverSocket.Stop(); Console.WriteLine(" >> " + "exit"); Console.ReadLine(); } } //Class to handle each client request separatly public class handleClinet { TcpClient clientSocket; string clNo; public void startClient(TcpClient inClientSocket, string clineNo) { this.clientSocket = inClientSocket; this.clNo = clineNo; Thread ctThread = new Thread(doChat); ctThread.Start(); } private void doChat() { int requestCount = 0; byte[] bytesFrom = new byte[10025]; string dataFromClient = null; Byte[] sendBytes = null; string serverResponse = null; string rCount = null; requestCount = 0; while ((true)) { try { requestCount = requestCount + 1; NetworkStream networkStream = clientSocket.GetStream(); networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient); rCount = Convert.ToString(requestCount); serverResponse = "Server to clinet(" + clNo + ") " + rCount; sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); Console.WriteLine(" >> " + serverResponse); } catch (Exception ex) { Console.WriteLine(" >> " + ex.ToString()); } } } } }