C# Multi threaded Client Socket programming

C# Multithreaded Client Socket Program here is a C# Windows based application and it can connect to the Server and send the message to the Server. The C# Server is multithreaded so we can connect more than one Client program to the Server .

The System.Net classes provide functionality that is similar to Microsoft WinInet API , it allows classes to communicate with other applications by using the Hypertext Transfer Protocol (HTTP), Transmission Control Protocol (TCP), User Datagram Protocol (UDP), and Socket Internet protocols. 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 section.

csharp-multithreaded-socket-programming-1

The C# Multi Threaded Socket Program has two sections.

1. C# Multi Threaded Server Socket Program

2. C# Multithreaded Client Socket Program

Multithreaded Client Socket Program

C# MultiThreaded Client Socket Program is a windows based application . Here the client program is connected to Server's PORT 8888 , and IP Address here we give Server Address as " 127.0.0.1 " , because Server and Client program run on the same machine.

clientSocket.Connect("127.0.0.1", 8888);

When the Client gets connected to the C# Server , the Server makes a separate thread for Client's communication . So we can connect more than one client and communicate at the same time.

Create a new C# Windows based application and put the following source code in the Project.

Full Source C#

How to run this program ?

The C# Multi Threaded Socket Program has two sections.

1. C# Multi Threaded Server Socket Program

2. C# Multithreaded Client Socket Program

Create C# Multi Threaded Server Socket Program and C# Multithreaded 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.Windows.Forms; using System.Net.Sockets; using System.Text; namespace WindowsApplication1 { public partial class Form1 : Form { System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); NetworkStream serverStream; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { msg("Client Started"); clientSocket.Connect("127.0.0.1", 8888); label1.Text = "Client Socket Program - Server Connected ..."; } private void button1_Click(object sender, EventArgs e) { NetworkStream serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); byte[] inStream = new byte[10025]; serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize); string returndata = System.Text.Encoding.ASCII.GetString(inStream); msg("Data from Server : " + returndata); } public void msg(string mesg) { textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg; } } }