Net-informations.com
SiteMap  | About    

VB.NET MultiThreaded Server Socket Programming

MultiThreaded Server Socket Program here is a VB.NET Console based application , that can handle multiple clients at the same time. You can see the basics of Socket Programming in the previous section , take a look at Socket Programming before you start this section.

The Multithreaded Socket Programming has two sections.

1. Multithreaded Server Socket Program

2. Multithreaded Client Socket Program

Here we create a Server Socket from TcpListener class and listen to PORT 8888 . When the server gets a request from Client , the Server pass the instance of the client request to a separate class handleClient . In handleClient class there is a Thread , handling the communication between the instance of Server side client and Client from outside .

For each request , in Server there is a new thread instant is create for communication , so we can connect more than one client at the same time to Server and 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

The Multithreaded Socket Programming has two sections.
1. Multithreaded Server Socket Program
2. Multithreaded Client Socket Program

You have to run Server program first and then Client program , then only you can communicate with Server and Client each other .








net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.