Chat client in VB.net

By: Issac Viewed: 172 times  Printer Friendly Format    


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 “127.0.0.1 “. Here we have given “127.0.0.1”, 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.

vb.net_chat_client.JPG

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)
        serverStream.Flush()
    End Sub

    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
        Else
            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 ..."
        msg()
        clientSocket.Connect("127.0.0.1", 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)
        serverStream.Flush()

        Dim ctThread As Threading.Thread = _
		 New Threading.Thread(AddressOf getMessage)
        ctThread.Start()
    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 = _
            System.Text.Encoding.ASCII.GetString(inStream)
            readData = "" + returndata
            msg()
        Next
    End Sub
End Class




Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. chat
View Tutorial          By: zahra zare at 2009-05-10 07:07:57

2. Okay, I have implemented this and it works, except
View Tutorial          By: Matthew at 2009-06-16 13:08:48

3. Nevermind, I was able to modify both applications
View Tutorial          By: Matthew at 2009-06-16 14:10:20

4. And how did you do that ?
Can you share it

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

5. Hii... pls how can I create a aerver with an inter
View Tutorial          By: WAZIRI at 2010-03-16 11:12:11

6. dd
View Tutorial          By: chandrasekar at 2010-03-29 23:24:46

7. Hi. I have implemented the code and controls just
View Tutorial          By: eibod at 2011-02-13 04:49:24

8. Hi. I have implemented the code and controls just
View Tutorial          By: eibod at 2011-02-13 04:58:44

9. Wow, incredible code!! With some brains this is a
View Tutorial          By: Dem0-n at 2011-06-11 07:10:12

10. Thanks a lot man with the server -side code.
View Tutorial          By: bot at 2011-12-03 15:04:28

11. what items do i add?
View Tutorial          By: Camo at 2015-09-30 23:02:10

12. Phylliscicky
View Tutorial          By: Phylliscicky at 2017-01-19 10:43:34