Client Socket Program sample in VB.net

By: Issac Emailed: 1598 times Printed: 2045 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 Client Socket Program we are gonna see in this article is continuation of the Server Socket Program. The Client Socket Program is also a Windows based application. Client connect to the Port 8888 of the Server Socket Program, and the IP Address (Computer Name) here we give as 127.0.0.1, because the Server and client running on the same machine.

clientSocket.Connect ("127.0.0.1", 8888) 

When client gets connected, it reads data from NetworkStream, and also writes to NetworkStream. When you start the client program you will get message client started. When we press the button at the bottom it sends a message to Server and also receives a message from Server.

Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes("Message from Client$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()

        Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
        Dim returndata As String = _
        System.Text.Encoding.ASCII.GetString(inStream)
        msg("Data from Server : " + returndata)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        msg("Client Started")
        clientSocket.Connect("127.0.0.1", 8888)
        Label1.Text = "Client Socket Program - Server Connected ..."
    End Sub

    Sub msg(ByVal mesg As String)
TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg
    End Sub
End Class

vb.net_client_socket_program.JPG

Before you run the Client Socket Program you should start Server Socket Program first. For more details of running this program, take a look at the VB.NET Socket Programming article I have posted before.

VB.net Home | All VB.net Tutorials | Latest VB.net 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

Comments(5)


1. View Comment

Hi,
really nice article. but still have one issue, that can i start socket programming with asp.net means for web application?
please replay me.........


View Tutorial          By: Yogesh Rishipathak at 2009-06-04 02:25:06
2. View Comment

Where is ur previous tutorial on VB.NET Socket Programming as mentioned above?

View Tutorial          By: Avipriya Sharma at 2009-07-20 10:05:15
3. View Comment

thank for your article it helps us to our studies in vb.net

View Tutorial          By: shane at 2010-07-21 19:12:22
4. View Comment

Hi Avipriya Sharma follow this link for my multi threaded socket programming aticle http://www.java-samples.com/showtutorial.php?tutorialid=1063 , hope this helps you

View Tutorial          By: Issac at 2011-06-27 00:17:21
5. View Comment

How i can comment in server?Some another message

View Tutorial          By: Youjizz at 2013-04-09 00:30:47

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 VB.net
Chat Server in VB.net
Chat client in VB.net
How to open and read an XML file in VB.net
How to create an XML file in VB.net
XML and VB.net
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in VB.net
Multi Threaded Server Socket Programming in VB.net
Multi threaded Socket Programming in VB.net
Client Socket Program sample in VB.net

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

More Latest News
Most Viewed Articles (in VB.net )
Your first VB.NET Crystal Reports - A step by step guide
String endswith, concat in VB.net
File stream operations in VB.net
“Using If with And” and Comparing two integers using If
The For Loop in VB.net
Assemblies, Solutions and Projects in VB .NET
Arrays and Dynamic Arrays in VB.net
Using Select Case in VB.net
For Each…Next Loop in VB.net
Stack data structure in VB.net
Insert Background Pictures in Excel 2007 from VB.NET
Using Excel 2007 Chart in VB.NET 2005
How to export from database to excel using VB.net
How to export from DataGridView to excel using VB.net
Server Socket Program sample in VB.net
Most Emailed Articles (in VB.net)
Nested If and Single line if statement
What is .NET Framework and the Common Language Runtime?
The Option and Imports Statements in VB .NET
Arrays and Dynamic Arrays in VB.net
For Loop in VB.net
For Each…Next Loop in VB.net
While Loop in VB.net
String equals, Copy to & Copy to in VB.net
String.Contains (), compare, clone in VB.net
String endswith, concat in VB.net
File directory operationst in VB.net
Vb.net binary reader and writer
Insert Pictures in Excel 2007 from VB.NET 2005
Excel 2007 Data Validation Input Message using VB.net
How to export from DataGridView to excel using VB.net