Multi Threaded Server Socket Programming in

By: Issac Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

        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

    End Sub

    Sub msg(ByVal mesg As String)
        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)
        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)
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = _
    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)
                Catch ex As Exception
                End Try

            End While

        End Sub
    End Class
End Module Home | All Tutorials | Latest Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in
Chat Server in
Chat client in
How to open and read an XML file in
How to create an XML file in
XML and
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in
Multi Threaded Server Socket Programming in
Multi threaded Socket Programming in
Client Socket Program sample in

More Tutorials in
Scope in
Unstructured Exception Handling in
Structured Exception Handling in
Creating Sub Procedures in
Creating Functions in
Passing a Variable Number of Arguments to Procedures in
Specifying Optional Arguments with default values in Procedures in
Preserving a Variable's Values between Procedure Calls in
Procedure Delegates in
Properties in
Understanding Scope in
Using Resume Next and Resume Line in
Using On Error GoTo 0 in
Getting an Exception's Number and Description in
Raising an Exception Intentionally in

More Latest News
Most Viewed Articles (in )
Creating a Windows Service in
What's New in VB .NET? A comparison of VB vs
Do Loop in
Your first VB.NET Crystal Reports - A step by step guide
The Option and Imports Statements in VB .NET
Arrays and Dynamic Arrays in
While Loop in
Stack data structure in
To read the entire worksheet in an Excel workbook through Code
Insert Pictures in Excel 2007 from VB.NET 2005
Multi Threaded Server Socket Programming in
How to create an XML file in VB.NET using Dataset
DataSet Class in
Creating a Windows Service Installer in
Structured Exception Handling in
Most Emailed Articles (in
File directory operationst in
Procedure Delegates in
Using Resume Next and Resume Line in
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
What is .NET Framework and the Common Language Runtime?
The Option and Imports Statements in VB .NET
Visual Basic Statements
Arrays and Dynamic Arrays in
Handling Dates and Times in
Queue data structure in text reader
Using Excel 2007 Chart in VB.NET 2005
Insert cell data in an Excel file using OLEDB in
Your first VB.NET Crystal Reports - A step by step guide