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


03-19-2012, 05:27 PM #31
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
Look at how the client receives them. It will be the same thing for the server.

03-19-2012, 05:46 PM #32
jackvald
Junior Member
**
Posts: 6 Threads:0 Joined: Feb 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
(03-19-2012, 05:27 PM)KoBE Wrote:  Look at how the client receives them. It will be the same thing for the server.
Ok, thanks a lot, I'll check it out.

03-21-2012, 02:51 AM #33
Mr.TwEaK
Junior Member
**
Posts: 4 Threads:0 Joined: Mar 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
Very nice detailed thread but if you would of taken the time to explain it would of been better then to just give everyone source and a video. All in all nice thread and thanks for the share.

03-23-2012, 07:22 PM #34
jackvald
Junior Member
**
Posts: 6 Threads:0 Joined: Feb 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
(03-19-2012, 05:27 PM)KoBE Wrote:  Look at how the client receives them. It will be the same thing for the server.
hi its me again.
just as you said, i used the client's code in order to receive data from the clients to the server's window (code shown below). Actually when i connect 1 client to the app i am able to receive the client's data, however when i connect 2 clients and run the app, both are detected but the server only receives data from one of the clients. how can i solve this in order to receive messages from both clients???
thanks in advance for your help!

Code:
Imports System.Net, System.Net.Sockets
Public Class frmServer
Dim serverSocket As Socket 'used to listen
Dim clientSocket As Socket 'used to talk to clients
Dim byteData(1023) As Byte

Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'create a new socket to lisen on
serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'endpoint tells the server where to listen
Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 5000)
'bind the endpoint to the serversocket before listening
serverSocket.Bind(IpEndPoint)
'start listening
serverSocket.Listen(5)
'begin accepting clients
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
End Sub
Private Sub OnAccept(ByVal ar As IAsyncResult)
'once accepted set the clientSocket to
'the new client and begin listening again
clientSocket = serverSocket.EndAccept(ar)
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
'add client to ListView
AddClient(clientSocket)
'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)
'once data is recieved create a temp socket
'to use while getting the data.
Dim client As Socket = ar.AsyncState
'stop receiveing on the clientsocket
client.EndReceive(ar)
'swap bytes into a new array
Dim bytesRec As Byte() = byteData
byteData(1023) = 0
'get the string representation
Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec)
'send message to sub to do work
Read(message)
'start recieving again
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
New AsyncCallback(AddressOf OnRecieve), clientSocket)
End Sub
Delegate Sub _Read(ByVal msg As String) 'delegate used to invoke
Private Sub Read(ByVal msg As String)
'RichTextBox1 was created on a different thread than
'what we are on. So an invoke is required
If InvokeRequired Then
Invoke(New _Read(AddressOf Read), msg)
Exit Sub
End If
RichTextBox1.Text &= msg
End Sub
Delegate Sub _AddClient(ByVal client As Socket) 'delege used to invoke AddCLient()
Private Sub AddClient(ByVal client As Socket)
'the listview was not made on the listening
'thread so we need to invoke this method
'before using it
If InvokeRequired Then
Invoke(New _AddClient(AddressOf AddClient), client)
Exit Sub
End If
'create a new listview item and add the IP to the list
Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
'.tag is an object that can be set to anything
'here we are placeing our socket there for use later
lvi.Tag = client
'add to the list
lsvClients.Items.Add(lvi)
End Sub
Private Sub Send(ByVal msg As String, ByVal client As Socket)
'get bytes to send
Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
'begin sending (notice the client is sent as an AsyncState)
client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
End Sub
Private Sub OnSend(ByVal ar As IAsyncResult)
'create a temp socket to use for our client
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 to the selected client
Send("Message from server", lsvClients.SelectedItems(0).Tag)
End Sub
End Class
This post was last modified: 03-23-2012, 08:33 PM by AceInfinity.

03-23-2012, 08:33 PM #35
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Multi-Client Server Async Socket Example
I fixed your post with code tags.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

03-24-2012, 05:05 PM #36
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Multi-Client Server Async Socket Example
Hey jackvald,

I will take some time and look through your code. I'll give you the same heads up that I do everyone: If you want to keep me from forgetting about this then feel free to send me a PM. I have a lot going on and tend to forget different things I'm working on. If you don't receive a response back from me by tommorow, please remind me. :D

04-09-2012, 03:07 AM #37
mesterkunna
Junior Member
**
Posts: 1 Threads:0 Joined: Apr 2012 Reputation: 0

RE: Multi-Client Server Async Socket Example
iam new and ican't download this file .
help Both Thumbs Up

04-09-2012, 03:09 AM #38
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Multi-Client Server Async Socket Example
Read the message posted on the board index.

http://tech.reboot.pro/showthread.php?tid=1861


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

04-10-2012, 09:39 PM #39
besimbicer
Junior Member
**
Posts: 24 Threads:3 Joined: Jul 2011 Reputation: 0

RE: Multi-Client Server Async Socket Example
sorry i need 50 post maybe add alternative link ?

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

RE: Multi-Client Server Async Socket Example
I would besimbicer, but the code is provided within the code tags. If you need help w/ the code please post what questions you have.


ii 


Forum Jump:


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


Users browsing this thread: 2 Guest(s)