Hello There, Guest!
View New Posts  |  View Today's Posts
[VB.Net] Multi-Client Server Async Socket Example

ii 
  • 4 Vote(s) - 4.5 Average


06-08-2011, 01:47 AM #1
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

Star  Multi-Client Server Async Socket Example
Hey TLF!

So here's the multi-client tutorial I promised. It's not actually a tutorial, it's more of a video going through the steps of creating it. I have included the source in code tags. You can also download the projects files via the attachment. The code in the code tags is not commented. However if you download the project files, I will try to comment everything that might be confusing. Hope you enjoy. It took me almost 30 mins to make the vid.




Server Code:
Code:
Imports System.Net, System.Net.Sockets
Public Class frmServer
    Dim serverSocket As Socket
    Dim clientSocket As Socket

    Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 8800)
        serverSocket.Bind(IpEndPoint)
        serverSocket.Listen(5)
        serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
    End Sub
    Private Sub OnAccept(ByVal ar As IAsyncResult)
        clientSocket = serverSocket.EndAccept(ar)
        serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
        AddClient(clientSocket)
    End Sub
    Delegate Sub _AddClient(ByVal client As Socket)
    Private Sub AddClient(ByVal client As Socket)
        If InvokeRequired Then
            Invoke(New _AddClient(AddressOf AddClient), client)
            Exit Sub
        End If
        Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
        lvi.Tag = client
        lsvClients.Items.Add(lvi)
    End Sub
    Private Sub Send(ByVal msg As String, ByVal client As Socket)
        Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
        client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
    End Sub
    Private Sub OnSend(ByVal ar As IAsyncResult)
        Dim client As Socket = ar.AsyncState
        client.EndSend(ar)
    End Sub
    Private Sub SendMessageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendMessageToolStripMenuItem.Click
        Send("Message from server", lsvClients.SelectedItems(0).Tag)
    End Sub
End Class

Client Code:
Code:
Imports System.Net, System.Net.Sockets
Public Class frmClient
    Dim clientSocket As Socket
    Dim byteData(1023) As Byte

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim ipAddress As IPAddress = ipAddress.Parse("127.0.0.1")
        Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, 8800)
        clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
    End Sub
    Private Sub OnConnect(ByVal ar As IAsyncResult)
        clientSocket.EndConnect(ar)
        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
                                  New AsyncCallback(AddressOf OnRecieve), clientSocket)
    End Sub
    Private Sub OnRecieve(ByVal ar As IAsyncResult)
        Dim client As Socket = ar.AsyncState
        client.EndReceive(ar)
        Dim bytesRec As Byte() = byteData
        Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec)
        Read(message)
        clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
                                  New AsyncCallback(AddressOf OnRecieve), clientSocket)
    End Sub
    Delegate Sub _Read(ByVal msg As String)
    Private Sub Read(ByVal msg As String)
        If InvokeRequired Then
            Invoke(New _Read(AddressOf Read), msg)
            Exit Sub
        End If
        RichTextBox1.Text &= msg
    End Sub

End Class


Attached Files
.rar   MultiSocket.rar (Size: 135.66 KB / Downloads: 3,992)
This post was last modified: 06-08-2011, 01:51 AM by KoBE.

06-08-2011, 02:13 AM #2
Deltron
Senior Member
***
Posts: 305 Threads:17 Joined: Jun 2011 Reputation: 6

RE: Multi-Client Server Async Socket Example
Great, detailed guide. Nicethread

06-08-2011, 02:25 AM #3
Sam Baker
I LOVE TLF!!!
*****
BSOD Crew
Posts: 1,525 Threads:76 Joined: Jun 2011 Reputation: 40

RE: Multi-Client Server Async Socket Example
OMG! You made a video?!
I ****ing Love you KoBe <3
Now i am become death,The destroyer of worlds ~oppenheimer


06-08-2011, 02:46 AM #4
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
I'm not gonna lie, I even watched the video over.. just so I could here me some Metallica. You're welcome. Let me know if you run into any problems.

06-08-2011, 07:29 AM #5
Sam Baker
I LOVE TLF!!!
*****
BSOD Crew
Posts: 1,525 Threads:76 Joined: Jun 2011 Reputation: 40

RE: Multi-Client Server Async Socket Example
(06-08-2011, 02:46 AM)KoBE Wrote:  I'm not gonna lie, I even watched the video over.. just so I could here me some Metallica. You're welcome. Let me know if you run into any problems.
Lol yeah i liked the song :D
Now i am become death,The destroyer of worlds ~oppenheimer


06-09-2011, 01:44 PM #6
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

RE: Multi-Client Server Async Socket Example
I am an old school Metallica fan, but that was an excellent choice.

06-09-2011, 02:07 PM #7
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
I'm an old school Metallica fan too, but I still like Load and Reload. St Anger.. not so much. Anyways back on topic. If you have any questions be sure to ask.

06-11-2011, 11:22 PM #8
RDCA
Senior Member
***
Posts: 278 Threads:10 Joined: Jun 2011 Reputation: 9

RE: Multi-Client Server Async Socket Example
Nice job bro, love your work.

11-11-2011, 08:34 PM #9
omar
Junior Member
**
Posts: 1 Threads:0 Joined: Nov 2011 Reputation: 0

RE: Multi-Client Server Async Socket Example
Great Job, But what if I need to send data back to the server from one of the clients? i have not been able to get it to work, any ideas?

11-12-2011, 04:44 AM #10
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
I thought I had an example of that in the forum. I am so bad about procrastinating so maybe I don't. I will try to remember to post an example.


ii 


Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
Exclamation  problem multi post + httpwebrequest read ip sharokurdi 1 1,325 11-11-2016, 06:39 PM
Last Post: AceInfinity
   Create your own IRC Client [C#/VB.Net] KoBE 185 121,207 10-01-2016, 06:57 PM
Last Post: AceInfinity
   IRC Client Class KoBE 110 74,998 10-01-2016, 06:52 PM
Last Post: AceInfinity
  Telnet Client AceInfinity 5 2,852 06-05-2016, 07:48 PM
Last Post: AceInfinity
   Twitch chat bot (irc client) dmgvol 11 19,416 08-24-2015, 11:59 PM
Last Post: Signal_20


Users browsing this thread: 1 Guest(s)