|
-
Jul 5th, 2007, 08:00 PM
#1
Re: [2005] TCP socket server example?
Each client is using a System.Net.Sockets.TcpClient, you will need to read its stream continouesly to find whenever something is incoming.
VB Code:
Private client As Net.Sockets.TcpClient
Private readBuffer(255) As Byte
Private Sub Button1_Click(...)
client = New Net.Sockets.TcpClient("somewhere", 0)
client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
End Sub
And then you will need a subroutine just like on the server:
VB Code:
Private Sub DoRead(ByVal ar As IAsyncResult)
Try
'EndRead returns the number of bytes that were recieved, so we will need to check so that something was recieved
Dim TotalBytes As Integer = client.GetStream.EndRead(ar)
If TotalBytes > 0
Dim strMessage As String = System.Text.Encoding.ASCII.GetString(readBuffer, 0, TotalBytes)
'Here is the place to do whatever you want with the incoming message: strMessage
'Calling this method again is important, it begins the read again.
client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
-
Jul 6th, 2007, 03:39 AM
#2
Thread Starter
Hyperactive Member
Re: [2005] TCP socket server example?
 Originally Posted by Atheist
Each client is using a System.Net.Sockets.TcpClient, you will need to read its stream continouesly to find whenever something is incoming.
VB Code:
Private client As Net.Sockets.TcpClient
Private readBuffer(255) As Byte
Private Sub Button1_Click(...)
client = New Net.Sockets.TcpClient("somewhere", 0)
client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
End Sub
And then you will need a subroutine just like on the server:
VB Code:
Private Sub DoRead(ByVal ar As IAsyncResult)
Try
'EndRead returns the number of bytes that were recieved, so we will need to check so that something was recieved
Dim TotalBytes As Integer = client.GetStream.EndRead(ar)
If TotalBytes > 0
Dim strMessage As String = System.Text.Encoding.ASCII.GetString(readBuffer, 0, TotalBytes)
'Here is the place to do whatever you want with the incoming message: strMessage
'Calling this method again is important, it begins the read again.
client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
Okay, so how do I know which client reads the data and receives it?
-
Jul 6th, 2007, 06:29 AM
#3
Re: [2005] TCP socket server example?
I dont really understand what you mean. All the code I posted in my last post should go in the client application. You would know who recieves the data because you would know who you sent the data too
-
Jul 6th, 2007, 07:25 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] TCP socket server example?
 Originally Posted by Atheist
I dont really understand what you mean. All the code I posted in my last post should go in the client application. You would know who recieves the data because you would know who you sent the data too 
I'm sorry I was misunderstood. I'll try to explain in another way. In the DoRead sub, I would like it from there to also return the actual name of the client you add when you use
Clients.Add("Mr Bean", client)
-
Jul 6th, 2007, 07:37 AM
#5
Re: [2005] TCP socket server example?
Aha, you would require the client to send his username along with the "Register" keyword.
I know that this technique was used in the MSDN example, lets say the client wants to send two things at once, both the "Register" keyword and his name, he would then send this:
"Register|Mr Bean"
to the server. When the server recieves this string, it splits it on each | character, and evaluates the first part (Which should always be the 'keyword'). Heres how the DoListen sub would look:
VB Code:
Private Sub DoListen()
'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
'If there is no connecting TcpClient, it will raise an exception but we will ignore this by catching it in a try statement and doing nothing with it.
'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
Dim sr As IO.StreamReader
Do
Try
Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
sr = New IO.StreamReader(client.GetStream)
'We split the incoming string by each | character, giving us an array of string elements where the first element is the 'Keyword'
Dim Data() As String = sr.ReadToEnd.Split("|"c)
sr.Close()
Select Case Data(0)
Case "Register"
'Data(0) was Register, so we know that the next element will be the name of the client
If Not Clients.ContainsKey(Data(1))
Clients.Add(Data(1), client)
End If
End Select
Catch
End Try
Loop
End Sub
-
Jul 6th, 2007, 07:39 AM
#6
Thread Starter
Hyperactive Member
Re: [2005] TCP socket server example?
 Originally Posted by Atheist
Aha, you would require the client to send his username along with the "Register" keyword.
I know that this technique was used in the MSDN example, lets say the client wants to send two things at once, both the "Register" keyword and his name, he would then send this:
"Register|Mr Bean"
to the server. When the server recieves this string, it splits it on each | character, and evaluates the first part (Which should always be the 'keyword'). Heres how the DoListen sub would look:
VB Code:
Private Sub DoListen()
'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
'If there is no connecting TcpClient, it will raise an exception but we will ignore this by catching it in a try statement and doing nothing with it.
'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
Dim sr As IO.StreamReader
Do
Try
Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
sr = New IO.StreamReader(client.GetStream)
'We split the incoming string by each | character, giving us an array of string elements where the first element is the 'Keyword'
Dim Data() As String = sr.ReadToEnd.Split("|"c)
sr.Close()
Select Case Data(0)
Case "Register"
'Data(0) was Register, so we know that the next element will be the name of the client
If Not Clients.ContainsKey(Data(1))
Clients.Add(Data(1), client)
End If
End Select
Catch
End Try
Loop
End Sub
Good job. Now, back to my question. In DoRead, how can I identify the name of the client I am reading from?
-
Jul 6th, 2007, 07:42 AM
#7
Re: [2005] TCP socket server example?
I dont understand the question . DoRead is the subroutine in the client application, it reads its own stream for incoming data. Any incoming data will be from the server.
The clients will not be sending data between eachother, everything goes through the server.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|