Sie sind auf Seite 1von 4

Multithreaded Socket Programming is the basic idea behind a TCP Chat Server communication.

In the previous section Multithreaded Socket Programming we can see a Multithreaded Server Socket Program communicate with more than one Client at the same time . But there the communication is happening only between Server to Client or Client to Server , there is no communication between Client to Client . In a Multithreaded Chat Server , a Client can communicate with any number of Clients , currently connected on the Chat Server .

Each Clients send messages to Server and the Server broadcast the message to all Clients currently connected to the Chat Server . From the following diagram you can see how a VB.NET TCP Chat Server is handling communication between Client to Client .

The VB.NET Multithreaded Chat Server program has two sections. 1. Chat Server 2. Chat Client VB.NET Chat Server The basic function of the Chat Server here is to listening for the connection request from Clients and when the Server get a message , it Broadcast the message to all Clients currently connected to the Server .

The VB.NET Multithreaded Chat Server Program has two sections. 1. Chat Server 2. Chat Client The Chat Server here is a VB.NET Console based application and is listening to the PORT 8888 for the connection request from clients . When the server gets a connection request , it add the name of the Client into a clientsList ( Here it is a Hashtable ) and create a new thread for communication with Server . When the Server get a message from any client , it select all the Clients fromclientsList and send the message to all Clients ( ie we can say Broadcast ) in the clientsList . So each Client can see the message each other and they can communicate through Chat Server. The client list we implemented here in a HashTable . The clientsList stores the Client Name ( ie the first message from Client ) and an instance of the Client Socket . When a Chat Client connected to Server , the Server create a new Thread for communication . Here we implement a ClasshandleClient for handling Client as a separate Thread . The Class handleClient has a function doChat() is handling the communication between the Server side Client Socket and the incoming Client Socket. When Server get a message from any of the currently connected Chat Client , the Server Broadcast the message to all Clients. Here we implement a function broadcast for sending messages to all Clients . VB.NET Chat Client The Chat Client is a Windows based Application and its main function is to send message to Chat Server.

The VB.NET Multithreaded Chat Server Program has two sections. 1. Chat Server 2. Chat Client The Chat Client here is to connect the PORT 8888 of the Chat Server in " 127.0.0.1 " . Here we give " 127.0.0.1 " , because Chat Server and Chat Client are running on the same machine . When we start the Chat Client program , we have to enter a User Name for identifying in Server . The Client program connect to the Chat Server and start a Thread for receive the messages from client, . Here we implement an infinite loop in the function getMessage() and call this function in a Thread .

Das könnte Ihnen auch gefallen