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


04-24-2012, 11:31 AM #41
diego089
Junior Member
**
Posts: 1 Threads:0 Joined: Apr 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
(06-08-2011, 01:47 AM)KoBE Wrote:  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


04-24-2012, 11:33 AM #42
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,863 Threads:495 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
Confused Did you forget to add your reply? :D

04-30-2012, 07:57 AM #43
tony123
Junior Member
**
Posts: 1 Threads:0 Joined: Apr 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
yeah... yeah.. nice one... that's really nice of you tomorrow and again. nice. very nice.
nice one, nice two and nice three by the way
give it all... give me something. and it's really nice
This post was last modified: 04-30-2012, 07:59 AM by tony123.

04-30-2012, 01:55 PM #44
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,863 Threads:495 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
You've been flagged (meaning your posts require approval). Please provide better responses. It's hard not to consider your post LQ.
This post was last modified: 04-30-2012, 01:55 PM by KoBE.

05-02-2012, 05:55 AM #45
umutduran
Junior Member
**
Posts: 1 Threads:0 Joined: May 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
thank you my friend this is very usefull

05-03-2012, 04:40 AM #46
D[A]rk[y]
Junior Member
**
Posts: 12 Threads:1 Joined: Jun 2011 Reputation: 0

RE: Multi-Client Server Async Socket Example
Nice Job.
I take the source and i go put the credit.

05-07-2012, 06:14 PM #47
onalvis
Junior Member
**
Posts: 1 Threads:0 Joined: May 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
Requiere this sorcue code..

05-16-2012, 06:24 AM #48
cgadjoro
Junior Member
**
Posts: 1 Threads:0 Joined: May 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
Nice job. Both Thumbs Up. But the video is too small can't see properly :(

05-16-2012, 04:11 PM #49
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,863 Threads:495 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
You could try to watch it on Youtube itself. There you can use full screen mode.

05-16-2012, 07:00 PM #50
AceInfinity
Developer
*******
Administrators
Posts: 9,721 Threads:1,025 Joined: Jun 2011 Reputation: 76

RE: Multi-Client Server Async Socket Example
(05-16-2012, 06:24 AM)cgadjoro Wrote:  Nice job. Both Thumbs Up. But the video is too small can't see properly :(
Too small? He even has zoomed sections in the video.


Microsoft MVP .NET Programming - (2012 - 2018)
Automation Programmer
 ▲
 ▲ ▲


ii 


Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
Exclamation  problem multi post + httpwebrequest read ip sharokurdi 1 1,573 11-11-2016, 06:39 PM
Last Post: AceInfinity
   Create your own IRC Client [C#/VB.Net] KoBE 185 132,907 10-01-2016, 06:57 PM
Last Post: AceInfinity
   IRC Client Class KoBE 110 83,154 10-01-2016, 06:52 PM
Last Post: AceInfinity
  Telnet Client AceInfinity 5 3,367 06-05-2016, 07:48 PM
Last Post: AceInfinity
   Twitch chat bot (irc client) dmgvol 11 20,497 08-24-2015, 11:59 PM
Last Post: Signal_20


Users browsing this thread: 2 Guest(s)