Chat client in

By: Issac Emailed: 1769 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

The Chat Client is a Windows based Application and its main function is to send message to Chat Server. The connection request is one which intiates all sort of acivities and Chat client is responsible for that.

The VB.NET Multithreaded Chat Server Program has two sections.

  • Chat Server
  • Chat Client

The Chat Client here is to connect the PORT 8888 of the Chat Server in “ “. Here we have given “”, because Chat Server and Chat Client are running on the same machine. When we start the Chat Client program, we have to enter a User Name just for identifying in Server. The Client program connect to the Chat Server and starts a Thread for receiving the messages from client, Here we implement an infinite loop in the function getMessage () and call this function in a Thread.


Create a new VB.NET Windows based project and put the source code in it..

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

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
    End Sub

    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
            TextBox1.Text = TextBox1.Text + _
			 Environment.NewLine + " >> " + readData
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        readData = "Conected to Chat Server ..."
        clientSocket.Connect("", 8888)
        'Label1.Text = "Client Socket Program - Server Connected ..."
        serverStream = clientSocket.GetStream()

        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)

        Dim ctThread As Threading.Thread = _
		 New Threading.Thread(AddressOf getMessage)
    End Sub

    Private Sub getMessage()
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            serverStream = clientSocket.GetStream()
            Dim buffSize As Integer
            Dim inStream(10024) As Byte
            buffSize = clientSocket.ReceiveBufferSize
            serverStream.Read(inStream, 0, buffSize)
            Dim returndata As String = _
            readData = "" + returndata
    End Sub
End Class 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


View Tutorial          By: zahra zare at 2009-05-10 07:07:57
2. View Comment

Okay, I have implemented this and it works, except for one thing. Running it from the compiler, when I kill the program by clicking on "X" icon in the upper right hand corner of the form, the form disappears but the program keeps running. How do you do a controlled log-off from the Chat Server and kill the multi-threaded segment prior to exiting?

View Tutorial          By: Matthew at 2009-06-16 13:08:48
3. View Comment

Nevermind, I was able to modify both applications to work well when the client decided to log off.

View Tutorial          By: Matthew at 2009-06-16 14:10:20
4. View Comment

And how did you do that ?
Can you share it with us ?

View Tutorial          By: Arsenio at 2009-07-18 23:01:00
5. View Comment

Hii... pls how can I create a aerver with an interface just like the client with it having a listen buttons and other buttons to help it connect.

View Tutorial          By: WAZIRI at 2010-03-16 11:12:11
6. View Comment

Hi. I have implemented the code and controls just the way you have explained above but everytime I try to connect to the server, it says "No connection could be made because the target machine actively refused it".

Does this have anything to do with our LAN at home?

View Tutorial          By: eibod at 2011-02-13 04:58:44
7. View Comment

Wow, incredible code!! With some brains this is a perfect start for my new trojan (: ty!

View Tutorial          By: Dem0-n at 2011-06-11 07:10:12
8. View Comment

Thanks a lot man with the server -side code.

View Tutorial          By: bot at 2011-12-03 15:04:28
9. View Comment

what items do i add?

View Tutorial          By: Camo at 2015-09-30 23:02:10

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
Send SMS using VB code
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