TechLifeForum
Multi-Client Server Async Socket Example - Printable Version

+- TechLifeForum (http://tech.reboot.pro)
+-- Forum: Programming and Development (http://tech.reboot.pro/forumdisplay.php?fid=15)
+--- Forum: .Net Framework Programming (http://tech.reboot.pro/forumdisplay.php?fid=17)
+--- Thread: Multi-Client Server Async Socket Example (/showthread.php?tid=86)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17


Multi-Client Server Async Socket Example - KoBE - 06-08-2011

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



RE: Multi-Client Server Async Socket Example - Deltron - 06-08-2011

Great, detailed guide. Nicethread


RE: Multi-Client Server Async Socket Example - Sam Baker - 06-08-2011

OMG! You made a video?!
I ****ing Love you KoBe <3


RE: Multi-Client Server Async Socket Example - KoBE - 06-08-2011

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.


RE: Multi-Client Server Async Socket Example - Sam Baker - 06-08-2011

(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



RE: Multi-Client Server Async Socket Example - _HAWK_ - 06-09-2011

I am an old school Metallica fan, but that was an excellent choice.


RE: Multi-Client Server Async Socket Example - KoBE - 06-09-2011

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.


RE: Multi-Client Server Async Socket Example - RDCA - 06-11-2011

Nice job bro, love your work.


RE: Multi-Client Server Async Socket Example - omar - 11-11-2011

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?



RE: Multi-Client Server Async Socket Example - KoBE - 11-12-2011

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.