VB.NET Chat Server

The primary function of the Chat Server in this context is to operate as a vigilant listener, attentively awaiting connection requests from various Clients. Once a Client establishes a connection with the Server, the Server diligently receives and processes incoming messages from the connected Clients.

The Chat Server's pivotal role is to facilitate seamless communication among multiple Clients by acting as a central hub. When a message is received from one Client, the Server swiftly orchestrates a Broadcast, relaying the message to all Clients currently linked to the Server.

This Broadcast mechanism ensures that any message originating from a single Client is promptly disseminated to all other Clients, promoting real-time and synchronized interactions within the chat environment. The Chat Server efficiently handles the distribution of messages, allowing all connected Clients to participate in the exchange of information, ideas, or thoughts.


The Chat Server's ability to manage and distribute messages effectively contributes to a smooth and efficient communication experience, making it an indispensable component of the chat application. Its robust listening and broadcasting capabilities ensure that all connected Clients remain well-informed and involved in the lively exchange of messages across the chat platform.

The VB.NET Multithreaded Chat Server Program has two sections.

1. Chat Server

2. Chat Client

Chat Server

The Chat Server, implemented as a VB.NET Console-based application, actively listens on PORT 8888 for incoming connection requests from clients. Upon receiving a connection request, the server appends the client's name to a clientsList, utilizing a Hashtable data structure. Additionally, a new thread is created to establish communication with the server.

As the server receives messages from clients, it selects all entries in the clientsList and broadcasts the received message to each client, thereby facilitating a broadcast-like communication. This allows each client to view and participate in the ongoing conversation through the Chat Server.

To manage the client connections, a Hashtable is employed to maintain the clientsList. This data structure stores the client name, typically the initial message sent by the client, along with an associated instance of the client socket.

When a Chat Client successfully connects to the server, the server initiates a dedicated thread to handle the client's communication. The server accomplishes this by utilizing a separate class called handleClient, which is responsible for managing client interactions as an independent thread. The handleClient class includes a doChat() function that handles communication between the server-side client socket and the incoming client socket.

Whenever the server receives a message from any of the currently connected chat clients, it broadcasts the message to all clients. This broadcasting functionality is implemented through a broadcast function, enabling efficient dissemination of messages to every client in the clientsList.

Create a new VB.NET Console based application and put the following source code into the Project.

Full Source VB.NET
Imports System.Net.Sockets Imports System.Text Module Module1 Dim clientsList As New Hashtable Sub Main() Dim serverSocket As New TcpListener(8888) Dim clientSocket As TcpClient Dim counter As Integer serverSocket.Start() msg("Chat Server Started ....") counter = 0 While (True) counter += 1 clientSocket = serverSocket.AcceptTcpClient() Dim bytesFrom(10024) As Byte Dim dataFromClient As String Dim networkStream As NetworkStream = _ clientSocket.GetStream() networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom) dataFromClient = _ dataFromClient.Substring(0, dataFromClient.IndexOf("$")) clientsList(dataFromClient) = clientSocket broadcast(dataFromClient + " Joined ", dataFromClient, False) msg(dataFromClient + " Joined chat room ") Dim client As New handleClinet client.startClient(clientSocket, dataFromClient, clientsList) End While clientSocket.Close() serverSocket.Stop() msg("exit") Console.ReadLine() End Sub Sub msg(ByVal mesg As String) mesg.Trim() Console.WriteLine(" >> " + mesg) End Sub Private Sub broadcast(ByVal msg As String, _ ByVal uName As String, ByVal flag As Boolean) Dim Item As DictionaryEntry For Each Item In clientsList Dim broadcastSocket As TcpClient broadcastSocket = CType(Item.Value, TcpClient) Dim broadcastStream As NetworkStream = _ broadcastSocket.GetStream() Dim broadcastBytes As [Byte]() If flag = True Then broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg) Else broadcastBytes = Encoding.ASCII.GetBytes(msg) End If broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length) broadcastStream.Flush() Next End Sub Public Class handleClinet Dim clientSocket As TcpClient Dim clNo As String Dim clientsList As Hashtable Public Sub startClient(ByVal inClientSocket As TcpClient, _ ByVal clineNo As String, ByVal cList As Hashtable) Me.clientSocket = inClientSocket Me.clNo = clineNo Me.clientsList = cList Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat) ctThread.Start() End Sub Private Sub doChat() 'Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 While (True) Try requestCount = requestCount + 1 Dim networkStream As NetworkStream = _ clientSocket.GetStream() networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize)) dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom) dataFromClient = _ dataFromClient.Substring(0, dataFromClient.IndexOf("$")) msg("From client - " + clNo + " : " + dataFromClient) rCount = Convert.ToString(requestCount) broadcast(dataFromClient, clNo, True) Catch ex As Exception MsgBox(ex.ToString) End Try End While End Sub End Class End Module
Download Chat Server Program

Chat Server Download

Chat Client Download

Refer to Chat Server Program for how to run this program .


The Chat Server, a VB.NET Console-based application, actively listens for connection requests on PORT 8888. It maintains a clientsList using a Hashtable, storing client names and associated client sockets. The server establishes separate threads for client communication using the handleClient class. As messages are received, the server broadcasts them to all clients, facilitating communication among clients via the Chat Server.