Results 1 to 40 of 65

Thread: [2005] TCP socket server example?

Hybrid View

  1. #1
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private client As Net.Sockets.TcpClient
    2. Private readBuffer(255) As Byte
    3.  
    4. Private Sub Button1_Click(...)
    5. client = New Net.Sockets.TcpClient("somewhere", 0)
    6. client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
    7. End Sub

    And then you will need a subroutine just like on the server:

    VB Code:
    1. Private Sub DoRead(ByVal ar As IAsyncResult)
    2. Try
    3.     'EndRead returns the number of bytes that were recieved, so we will need to check so that something was recieved
    4.     Dim TotalBytes As Integer = client.GetStream.EndRead(ar)
    5.     If TotalBytes  > 0
    6.         Dim strMessage As String = System.Text.Encoding.ASCII.GetString(readBuffer, 0, TotalBytes)
    7.        
    8.         'Here is the place to do whatever you want with the incoming message: strMessage
    9.    
    10.         'Calling this method again is important, it begins the read again.
    11.         client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
    12. Catch e As Exception
    13.     MessageBox.Show(e.Message)
    14. End Try
    15. End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: [2005] TCP socket server example?

    Quote 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:
    1. Private client As Net.Sockets.TcpClient
    2. Private readBuffer(255) As Byte
    3.  
    4. Private Sub Button1_Click(...)
    5. client = New Net.Sockets.TcpClient("somewhere", 0)
    6. client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
    7. End Sub

    And then you will need a subroutine just like on the server:

    VB Code:
    1. Private Sub DoRead(ByVal ar As IAsyncResult)
    2. Try
    3.     'EndRead returns the number of bytes that were recieved, so we will need to check so that something was recieved
    4.     Dim TotalBytes As Integer = client.GetStream.EndRead(ar)
    5.     If TotalBytes  > 0
    6.         Dim strMessage As String = System.Text.Encoding.ASCII.GetString(readBuffer, 0, TotalBytes)
    7.        
    8.         'Here is the place to do whatever you want with the incoming message: strMessage
    9.    
    10.         'Calling this method again is important, it begins the read again.
    11.         client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
    12. Catch e As Exception
    13.     MessageBox.Show(e.Message)
    14. End Try
    15. End Sub
    Okay, so how do I know which client reads the data and receives it?

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: [2005] TCP socket server example?

    Quote 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)


  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub DoListen()
    2.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    3.         '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.
    4.         'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    5.         Dim sr As IO.StreamReader
    6.         Do
    7.             Try
    8.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    9.                 sr = New IO.StreamReader(client.GetStream)
    10.                 'We split the incoming string by each | character, giving us an array of string elements where the first element is the 'Keyword'
    11.                 Dim Data() As String = sr.ReadToEnd.Split("|"c)
    12.                 sr.Close()
    13.                 Select Case Data(0)
    14.                     Case "Register"
    15.                         'Data(0) was Register, so we know that the next element will be the name of the client
    16.                         If Not Clients.ContainsKey(Data(1))
    17.                             Clients.Add(Data(1), client)
    18.                         End If
    19.                 End Select
    20.             Catch
    21.             End Try
    22.         Loop
    23.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: [2005] TCP socket server example?

    Quote 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:
    1. Private Sub DoListen()
    2.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    3.         '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.
    4.         'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    5.         Dim sr As IO.StreamReader
    6.         Do
    7.             Try
    8.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    9.                 sr = New IO.StreamReader(client.GetStream)
    10.                 'We split the incoming string by each | character, giving us an array of string elements where the first element is the 'Keyword'
    11.                 Dim Data() As String = sr.ReadToEnd.Split("|"c)
    12.                 sr.Close()
    13.                 Select Case Data(0)
    14.                     Case "Register"
    15.                         'Data(0) was Register, so we know that the next element will be the name of the client
    16.                         If Not Clients.ContainsKey(Data(1))
    17.                             Clients.Add(Data(1), client)
    18.                         End If
    19.                 End Select
    20.             Catch
    21.             End Try
    22.         Loop
    23.     End Sub
    Good job. Now, back to my question. In DoRead, how can I identify the name of the client I am reading from?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width