TechLifeForum

Full Version: Multi-Client Server Async Socket Example
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
Great, detailed guide. Nicethread
OMG! You made a video?!
I ****ing Love you KoBe <3
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, 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
I am an old school Metallica fan, but that was an excellent choice.
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.
Nice job bro, love your work.
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?
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.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17