Multi Threaded Server Socket Programming in

By: Issac Emailed: 1673 times Printed: 2153 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 )
Your first VB.NET Crystal Reports - A step by step guide
Preserving a Variable's Values between Procedure Calls in
While Loop in
How to export from database to excel using
Client Socket Program sample in
Creating Tree Views in Code using
The For Loop in
What is .NET Framework and the Common Language Runtime?
Data types in
Arrays and Dynamic Arrays in
Do Loop in
For Each…Next Loop in
Sub Procedures and Functions in
Stack data structure in
Implicit & explicit type Conversion in
Most Emailed Articles (in
Send SMS using VB code
String equals, Copy to & Copy to in
How to export from database to excel using
A tutorial on Chat Server and Chat Client in
Your first VB.NET Crystal Reports - A step by step guide
Socket Programming in
Handling Timer Events - and Creating an Alarm Clock in
File Extensions Used in VB .NET
Using Select Case in
For Loop in
How To Connect To A MySql Database in
Stack data structure in
String.Contains (), compare, clone in
String Split function in
Create an Excel 2007 file in VB.NET 2005