Multi Threaded Server Socket Programming in VB.net

By: Issac Viewed: 192 times  Printer Friendly Format    


Multithreaded Server Socket Progam is a Console application which can easily handle multiple clients at the same instant, we need to create a Server Socket from TcpListener class and listen to PORT 8888. When the server gets a request from Client, the Server passes the instance of the client request to a separate class handleClient. In handleClient class there is a Thread, which handles the communication between the instance of Server side client and Client from outside.

For each request from the client, in Server there is a new thread instant is created for communication, so we can connect more than one client at the same time to Server and enable them to communicate independently.

Create a new VB.NET Console Application project and put the following source code in the project.

Imports System.Net.Sockets
Imports System.Text
Module Module1
    Sub Main()
        Dim serverSocket As New TcpListener(8888)
        Dim clientSocket As TcpClient
        Dim counter As Integer

        serverSocket.Start()
        msg("Server Started")
        counter = 0
        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()
            msg("Client No:" + Convert.ToString(counter) + " started!")
            Dim client As New handleClinet
            client.startClient(clientSocket, Convert.ToString(counter))
        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

    Public Class handleClinet
        Dim clientSocket As TcpClient
        Dim clNo As String
        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub
        Private Sub doChat()
            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)
            serverResponse = "Server to clinet(" + clNo + ") " + rCount
                    sendBytes = Encoding.ASCII.GetBytes(serverResponse)
                    networkStream.Write(sendBytes, 0, sendBytes.Length)
                    networkStream.Flush()
                    msg(serverResponse)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try

            End While

        End Sub
    End Class
End Module



Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial