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 .
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.
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());
}
}
}
}
}