How to C# Chat Server
The basic function of the Chat Server here is to listen to the incoming request from Clients, and when the Server got a message , it Broadcast the message to all the Clients currently connected to the Chat Server .
In the previous section C# Multi Threaded Socket Program we saw a Multithreaded Server Socket Program communicate with more than one Clients at the same time .
The C# Chat Server Program has two sections.
1. Chat Server
Chat Server
The Chat Server here is a C# Console Based Application and is listen to the PORT 8888 for the connection request from clients . When the server got a connection request, it add the name of the Client into a clientsList ( Here it is a Hashtable ) and create a new thread for communicate with the Server .
When the Server gets a message from any client , it select all the Clients from the clientsList and send the message ( Broadcast ) to all Clients ( ie we can say Broadcast ) in the clientsList . So all Client can see the message of each others and they can communicate through Server.
The client list we implemented as a HashTable . The clientsList stores the Client Name ( ie here the first message from Client ) and create an instance of the Client Socket .
When a Client connected to Server , the Server create a new Thread for communication . Here we implement a Class handleClient for handling Client as a separate Thread . The Class handleClient has a function doChat() is handling the communication between the Server side Client.
When Server gets a message from any of the connected Chat Client , the Server Broadcast the message to all Clients. Here we implement a function broadcast for sending messages to all Clients .
Create a new C# Console Based Application and put the following source code into the Project.
Full Source C#The C# Chat Server Program has two sections.
1. Chat Server
How to run Chat Server program ?
Create the C# Chat Server and C# Chat Client are two separate C# projects and compile and build the program. Open a DOS prompt and run the Server Program first and then run the Client program .
In the Client program, Enter a Chat name and click " Connect to Server " button . Then you can see the message in the Server program User "Joined Chat Room" . Similarly you can connect more than one Clients at the same time and start chatting each other. If you plan to run more than one client, it is better to copy the .exe file in separate folders and run from the .exe file.
Full Source C#- How to send email from C#
- How to send email with attachment from C#
- How to send html email from C#
- How to send cdo email from C#
- How to find hostname of a computer
- How to find IP Adress of a computer
- How to read URL Content from webserver
- How to C# Socket programming
- C# Server Socket program
- C# Client Socket program
- C# Multi threaded socket programming
- C# Multi threaded Server Socket programming
- C# Multi threaded Client Socket programming
- How to C# Chat server programming
- How to C# Chat Client
- How to web browser in C#
- No connection could be made because the target machine actively refused it
- System.Net.Sockets.SocketException (0x80004005)
- C# HttpClient - HTTP requests with HttpClient in C#
- C# HttpClient status code