VB.NET Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded

VB.NET TCP/IP Client/Server : How to create Multi Client Server Chat Application using MultiThreaded Server Socket Program and multi client

VB.NET Multi Server Client - MultiThread server client is allow to send message using multi client to the server, the server can send message to all client, and will display into all client if they connected to the server. you can connect to the server more then one client at the same time. Before this session, we have create one project about TCP/IP client/server only can for one client and one server.

Please read :
  1. Client Server Chat Application TCP/IP Client/Server for One Client
  2. How to create Simple Internet Download Manager + BackgroundWorker

TCP/IP CLient and Server can handle multi client at the same time. we will use System.Net.Sockets, System.Net and System.IO namespace. so, lets create it.

TCP/IP Server Project

On your visual studio, create new project and rename it with "TCPServers", and at the Form1.vb just design look like this one :

 Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded

Create new Class for Read All Client (ClassForClient.vb)

Leave the Form1.vb and create a new class "ClassForClient.vb" and write all source code below :

Imports System.Net.Sockets
Imports System.IO
Public Class ClassforClient
    Public Event getMessage(ByVal str As String)
    Public Event clientLogout(ByVal client As ClassforClient)
    Private sendMessage As StreamWriter
    Private listClient As TcpClient
    Sub New(ByVal forClient As TcpClient)
        listClient = forClient
        listClient.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf ReadAllClient, Nothing)
    End Sub
    Private Sub ReadAllClient()
        Try
            RaiseEvent getMessage(New StreamReader(listClient.GetStream).ReadLine)
            listClient.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf ReadAllClient), Nothing)
        Catch ex As Exception
            RaiseEvent clientLogout(Me)
        End Try
    End Sub
    Public Sub Send(ByVal Messsage As String)
        sendMessage = New StreamWriter(listClient.GetStream)
        sendMessage.WriteLine(Messsage)
        sendMessage.Flush()
    End Sub
End Class

Back to the Form1.vb

Source code TCP/IP Server (Form1.vb)

Just write all source code below:

Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
    Dim Listning As TcpListener
    Dim Allclient As TcpClient
    Dim clientList As New List(Of ClassforClient)
    Dim pReader As StreamReader
    Dim pClient As ClassforClient
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Listning = New TcpListener(IPAddress.Any, 3818)
        Listning.Start()
        UpdateList("Server Starting", False)
        Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
    End Sub
    ' create a delegate
    Delegate Sub _cUpdate(ByVal str As String, ByVal relay As Boolean)
    Sub UpdateList(ByVal str As String, ByVal relay As Boolean)
        On Error Resume Next
        If InvokeRequired Then
            Invoke(New _cUpdate(AddressOf UpdateList), str, relay)
        Else
            TextBox1.AppendText(str & vbNewLine)
            ' if relay we will send a string
            If relay Then send(str)
        End If
    End Sub
    Sub send(ByVal str As String)
        For x As Integer = 0 To clientList.Count - 1
            Try
                clientList(x).Send(str)
            Catch ex As Exception
                clientList.RemoveAt(x)
            End Try
        Next
    End Sub
    Sub AcceptClient(ByVal ar As IAsyncResult)
        pClient = New ClassforClient(Listning.EndAcceptTcpClient(ar))
        AddHandler(pClient.getMessage), AddressOf MessageReceived
        AddHandler(pClient.clientLogout), AddressOf ClientExited
        clientList.Add(pClient)
        UpdateList("New Client Joined!", True)
        Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
    End Sub
    Sub MessageReceived(ByVal str As String)
        UpdateList(str, True)
    End Sub
    Sub ClientExited(ByVal client As ClassforClient)
        clientList.Remove(client)
        UpdateList("Client Exited!", True)
    End Sub
    Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            UpdateList("Server says : " & TextBox2.Text, True)
            TextBox2.Clear()
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        UpdateList("Server says : " & TextBox2.Text, True)
        TextBox2.Clear()
    End Sub
End Class

Yes, we have created for the server, now we will create one client and it's can use for another client.

Create TCP/IP Client (CLient.vb)

After the server has done, we will create new project in our visual studio and rename it with "TCPClient", and at the Form1.vb just design it look like this images :

Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded
At the Form1.vb just write all source code below :

Source Code TCP/IP Client

Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
    Dim client As TcpClient
    Dim sWriter As StreamWriter
    Dim NIckFrefix As Integer = New Random().Next(1111, 9999)
    Sub xLoad() Handles Me.Load
        Me.Text &= " " & NIckFrefix
    End Sub
    Delegate Sub _xUpdate(ByVal str As String)
    Sub xUpdate(ByVal str As String)
        If InvokeRequired Then
            Invoke(New _xUpdate(AddressOf xUpdate), str)
        Else
            TextBox3.AppendText(str & vbNewLine)
        End If
    End Sub

    Sub read(ByVal ar As IAsyncResult)
        Try
            xUpdate(New StreamReader(client.GetStream).ReadLine)
            client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing)

        Catch ex As Exception
            xUpdate("You have disconnecting from server")
            Exit Sub
        End Try
    End Sub
    Private Sub send(ByVal str As String)
        Try
            sWriter = New StreamWriter(client.GetStream)
            sWriter.WriteLine(str)
            sWriter.Flush()
        Catch ex As Exception
            xUpdate("You're not server")
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "Connect" Then
            Try
                client = New TcpClient(TextBox1.Text, CInt(TextBox2.Text))
                client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), Nothing)
                Button1.Text = "Disconnect"
            Catch ex As Exception
                xUpdate("Can't connect to the server!")
            End Try
        Else
            client.Client.Close()
            client = Nothing
            Button1.Text = "Connect"
        End If
    End Sub
    Private Sub TextBox4_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox4.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            send(NIckFrefix & " says : " & TextBox4.Text)
            TextBox4.Clear()
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        send(NIckFrefix & " says : " & TextBox4.Text)
        TextBox4.Clear()
    End Sub
End Class

Now, to debugging our simple Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded program, first run the server, after success then run the client, you can run the client.exe from debug folder, and you can open the client exe for many form at he same time.

On the client form, just write at the Server label with : 127.0.0.1 (your server IP) and Port Number : 3818 (i'm using this port)
Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded
If you still confused with tutorial above, just watch video tutorial below :

Video tutorial How to create Multi Client Server Chat Application using VB.NET




Informations :
Download Full Source code Multi Client Server TCP/IP Clienr/Server application (server)
Download Full Source code Multi Client Server TCP/IP Clienr/Server application (Client)

See you next sessions.

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: VB.NET Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded
VB.NET Multi Client Server Chat Application TCP/IP Client/Server MultiThreaded
VB.NET TCP/IP Client/Server : How to create Multi Client Server Chat Application using MultiThreaded Server Socket Program and multi client
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ1rEiiHN1SOsdzlKQgBg2oxUidcsYPX83v90DqQGtdlKX2RtQyoImUtxPgRmfROdrpuueSwdZJ7iUdrnj82OlXtVWbr2W61fZjQSVhwv4K0_yE14y-_-Kp2MhyZCm0gdLZYQrPvzUadM/s320/tcp-ip-client-server-vb-net-1.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ1rEiiHN1SOsdzlKQgBg2oxUidcsYPX83v90DqQGtdlKX2RtQyoImUtxPgRmfROdrpuueSwdZJ7iUdrnj82OlXtVWbr2W61fZjQSVhwv4K0_yE14y-_-Kp2MhyZCm0gdLZYQrPvzUadM/s72-c/tcp-ip-client-server-vb-net-1.jpg
KODE AJAIB
https://scqq.blogspot.com/2016/07/vbnet-multi-client-server-chat.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/07/vbnet-multi-client-server-chat.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy