Server Socket Program sample 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

The Server Socket Program is a VB.NET Console based Application. This program acts as a Server and listens to client’s request. We have to assign Port 8888 for the Server Socket, it is an instance of the VB.NET Class TcpListener, and call its start () method.

Dim serverSocket As New TcpListener (8888)
serverSocket.Start ()

Next we are creating an infinite loop for continuous communication to Client. When Server gets the request, it reads the data from NetworkStream and also writes the response to NetworkStream. For doing this create a new VB.NET Console Application Project and put the following source code into project.

Imports System.Net.Sockets
Imports System.Text
Module Module1
    Sub Main()
        Dim serverSocket As New TcpListener(8888)
        Dim requestCount As Integer
        Dim clientSocket As TcpClient
        msg("Server Started")
        clientSocket = serverSocket.AcceptTcpClient()
        msg("Accept connection from client")
        requestCount = 0

        While (True)
                requestCount = requestCount + 1
                Dim networkStream As NetworkStream = _
                Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                Dim dataFromClient As String = _
                dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                msg("Data from client -  " + dataFromClient)
                Dim serverResponse As String = _
                    "Server response " + Convert.ToString(requestCount)
                Dim sendBytes As [Byte]() = _
                networkStream.Write(sendBytes, 0, sendBytes.Length)
            Catch ex As Exception
            End Try
        End While

    End Sub

    Sub msg(ByVal mesg As String)
        Console.WriteLine(" >> " + mesg)
    End Sub
End Module


This server socket is in only a part of the socket program,we need a client to test it fully, we will see about it in detail in the client socket programming tutorial, you should first start the Server Socket Program and then start the Client Socket Program. 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


1. View Comment

hi there, i was testing your code-samples,
but i keep getting errors on the server after the client disconected
is there any way to avoid this?
like to check if the client disconected and then quit the infinite loop, i tried checking the Connected attribute of the tcpClient but even though the client disconected it still "thinks" that it's connected though...that means, no success so far

can you give me a hand with this?

View Tutorial          By: Douglas at 2011-04-07 22:35:56
2. View Comment

I have error on "serversocket", highlights it everytime I am pasting the code to my project. here's the error statement :

Public Sub New(port As Integer)' is obsolete: 'This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port)

View Tutorial          By: stephen at 2012-07-14 19:40:03
3. View Comment

Yes, you can use the newer one as follows
Dim tcpListener As New TcpListener(System.Net.IPAddress.Parse(""), portNumber)

View Tutorial          By: John at 2012-08-11 01:40:46

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
The Option and Imports Statements in VB .NET
A tutorial on Chat Server and Chat Client in
Operators in
How to export from DataGridView to excel using
Creating Tree Views in Code using
Using Resume Next and Resume Line in
Arrays and Dynamic Arrays in
How To Connect To A MySql Database in
Insert cell data in an Excel file using OLEDB in
How to read URL Content through code
Handling Timer Events - and Creating an Alarm Clock in
Specifying Optional Arguments with default values in Procedures in
Changes in Controls from VB6 to
File Extensions Used in VB .NET
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