Page 1 of 2 12 LastLast
Results 1 to 40 of 65

Thread: [2005] TCP socket server example?

  1. #1

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

    [2005] TCP socket server example?

    I'd like a TCP socket server example. I need it, and I haven't been able to find it anywhere. Only a few useless samples.

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

    Re: [2005] TCP socket server example?

    The MSDN 101 Samples has a pretty good example.
    Anyways, heres one Ive made for you:
    VB Code:
    1. Dim listener As Net.Sockets.TcpListener
    2.     Dim listenThread As Threading.Thread
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         listener = New Net.Sockets.TcpListener(Net.IPAddress.Any, 32111)
    5.         listener.Start()
    6.         listenThread = New Threading.Thread(AddressOf DoListen)
    7.         listenThread.IsBackground = True
    8.         listenThread.Start()
    9.     End Sub
    10.  
    11.     Private Sub DoListen()
    12.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    13.         '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.
    14.         'On the other hand, If a client has connected, it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    15.         Dim sr As IO.StreamReader
    16.         Do
    17.             Try
    18.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    19.                 sr = New IO.StreamReader(client.GetStream)
    20.                 MessageBox.Show(sr.ReadToEnd)
    21.                 sr.Close()
    22.             Catch
    23.             End Try
    24.         Loop
    25.     End Sub

    Ive just put some badly written explanations in the DoListen sub, I think you can figure the rest out
    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)

  3. #3

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    The MSDN 101 Samples has a pretty good example.
    Anyways, heres one Ive made for you:
    VB Code:
    1. Dim listener As Net.Sockets.TcpListener
    2.     Dim listenThread As Threading.Thread
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         listener = New Net.Sockets.TcpListener(Net.IPAddress.Any, 32111)
    5.         listener.Start()
    6.         listenThread = New Threading.Thread(AddressOf DoListen)
    7.         listenThread.IsBackground = True
    8.         listenThread.Start()
    9.     End Sub
    10.  
    11.     Private Sub DoListen()
    12.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    13.         '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.
    14.         'On the other hand, If a client has connected, it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    15.         Dim sr As IO.StreamReader
    16.         Do
    17.             Try
    18.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    19.                 sr = New IO.StreamReader(client.GetStream)
    20.                 MessageBox.Show(sr.ReadToEnd)
    21.                 sr.Close()
    22.             Catch
    23.             End Try
    24.         Loop
    25.     End Sub

    Ive just put some badly written explanations in the DoListen sub, I think you can figure the rest out
    Uuuuh! Good job! However, how to make it multi client compatible?

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

    Re: [2005] TCP socket server example?

    It is multiclient compatible.
    Alot of clients can send data to the server at the same time.
    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)

  5. #5

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    It is multiclient compatible.
    Alot of clients can send data to the server at the same time.
    Yes, but what I mean, is that how to make the server be able to reply to a specific client of all the clients connected?

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

    Re: [2005] TCP socket server example?

    Aha. Wait 1 sec rewriting example...
    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)

  7. #7

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    Aha. Wait 1 sec rewriting example...
    Nice. Thanks.

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

    Re: [2005] TCP socket server example?

    VB Code:
    1. Dim listener As Net.Sockets.TcpListener
    2.     Dim listenThread As Threading.Thread
    3.     Dim Clients As New Dictionary(Of String, Net.Sockets.TcpClient)
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         listener = New Net.Sockets.TcpListener(Net.IPAddress.Any, 32111)
    6.         listener.Start()
    7.         listenThread = New Threading.Thread(AddressOf DoListen)
    8.         listenThread.IsBackground = True
    9.         listenThread.Start()
    10.     End Sub
    11.  
    12.     Private Sub DoListen()
    13.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    14.         '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.
    15.         'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    16.         Dim sr As IO.StreamReader
    17.         Do
    18.             Try
    19.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    20.                 sr = New IO.StreamReader(client.GetStream)
    21.                 If sr.ReadToEnd = "Register" Then
    22.                     Clients.Add("Mr Bean", client)
    23.                 End If
    24.                 sr.Close()
    25.             Catch
    26.             End Try
    27.         Loop
    28.     End Sub
    29.  
    30.     Private Function SendToUser(ByVal user As String, ByVal data As String) As Boolean
    31.         If Clients.ContainsKey(user) Then
    32.             Dim sw As New IO.StreamWriter(Clients.Item(user).GetStream)
    33.             sw.Write(data)
    34.             sw.Flush()
    35.             sw.Close()
    36.             Return True
    37.         Else
    38.             Return False
    39.         End If
    40.     End Function

    In this example, if a client sends "Register" to the server, the server adds the client to a Dictionary along with a unique string that identifies it (altough in this case, the string will not be unique, but you will need to change that). And the SendToUser function pretty much speaks for itself.
    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)

  9. #9

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    VB Code:
    1. Dim listener As Net.Sockets.TcpListener
    2.     Dim listenThread As Threading.Thread
    3.     Dim Clients As New Dictionary(Of String, Net.Sockets.TcpClient)
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         listener = New Net.Sockets.TcpListener(Net.IPAddress.Any, 32111)
    6.         listener.Start()
    7.         listenThread = New Threading.Thread(AddressOf DoListen)
    8.         listenThread.IsBackground = True
    9.         listenThread.Start()
    10.     End Sub
    11.  
    12.     Private Sub DoListen()
    13.         'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over.
    14.         '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.
    15.         'If a client has connected it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox.
    16.         Dim sr As IO.StreamReader
    17.         Do
    18.             Try
    19.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    20.                 sr = New IO.StreamReader(client.GetStream)
    21.                 If sr.ReadToEnd = "Register" Then
    22.                     Clients.Add("Mr Bean", client)
    23.                 End If
    24.                 sr.Close()
    25.             Catch
    26.             End Try
    27.         Loop
    28.     End Sub
    29.  
    30.     Private Function SendToUser(ByVal user As String, ByVal data As String) As Boolean
    31.         If Clients.ContainsKey(user) Then
    32.             Dim sw As New IO.StreamWriter(Clients.Item(user).GetStream)
    33.             sw.Write(data)
    34.             sw.Flush()
    35.             sw.Close()
    36.             Return True
    37.         Else
    38.             Return False
    39.         End If
    40.     End Function

    In this example, if a client sends "Register" to the server, the server adds the client to a Dictionary along with a unique string that identifies it (altough in this case, the string will not be unique, but you will need to change that). And the SendToUser function pretty much speaks for itself.
    Very impressive! You keep surprising me!

    Now, what if I want to send data to all clients connected?

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

    Re: [2005] TCP socket server example?

    You would do something like this:

    VB Code:
    1. Private Sub SendToAll(ByVal data As String)
    2.         Dim sw As IO.StreamWriter
    3.         For Each c As Net.Sockets.TcpClient In Clients.Values
    4.             sw = New IO.StreamWriter(c.GetStream)
    5.             sw.Write(data)
    6.             sw.Flush()
    7.             sw.Close()
    8.         Next
    9.     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)

  11. #11

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    You would do something like this:

    VB Code:
    1. Private Sub SendToAll(ByVal data As String)
    2.         Dim sw As IO.StreamWriter
    3.         For Each c As Net.Sockets.TcpClient In Clients.Values
    4.             sw = New IO.StreamWriter(c.GetStream)
    5.             sw.Write(data)
    6.             sw.Flush()
    7.             sw.Close()
    8.         Next
    9.     End Sub
    That's great! I'll test that tomorrow, and reply with my rating from 1 to 10, among with any problems I might experience. Thanks for it! It seems suitable! +REP!

  12. #12

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

    Thumbs down Re: [2005] TCP socket server example?

    Quote Originally Posted by Mathiaslylo
    That's great! I'll test that tomorrow, and reply with my rating from 1 to 10, among with any problems I might experience. Thanks for it! It seems suitable! +REP!
    So what if I want the code to have some kind of event that triggers when one of the clients receive data?

  13. #13
    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)

  14. #14

    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?

  15. #15
    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)

  16. #16

    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)


  17. #17
    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)

  18. #18

    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?

  19. #19
    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)

  20. #20

    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 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.
    Well, I'll try to modify your code to explain.

    First, let's say 2 clients are connected. "John", and "Mathy". This sub then occurs, since one of those 2 clients receives data, which causes the "DoRead" sub to be called. However, inside the DoRead sub, how do I know if it was "John" or "Mathy" that received the data?

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Mathiaslylo
    Well, I'll try to modify your code to explain.

    First, let's say 2 clients are connected. "John", and "Mathy". This sub then occurs, since one of those 2 clients receives data, which causes the "DoRead" sub to be called. However, inside the DoRead sub, how do I know if it was "John" or "Mathy" that received the data?
    I take it that John and Mathy are both on their own pc's, then they will be running their own instances of the client application. Two clients will not be sharing the same application, and therefor not the same DoRead subroutine
    I hope that answer was clear
    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)

  22. #22

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    I take it that John and Mathy are both on their own pc's, then they will be running their own instances of the client application. Two clients will not be sharing the same application, and therefor not the same DoRead subroutine
    I hope that answer was clear
    Well, it wasn't You see, I still can't determine who is who.
    I'd like the function to be modified something like:

    Private Sub DoRead(ByVal ar As IAsyncResult, byval User as string)

    Where User is the user receiving data?

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

    Re: [2005] TCP socket server example?

    So you're saying that there will only be 1 client application that will be used by many users? If so:
    Every message that the server sends should consist of atleast two parts: The first part is the 'Keyword' (defines what the message actually is), and the second part should be to which user the message is addressed. Something like this:

    VB Code:
    1. "Message|Mr Bean|Go to bed!"

    Could be sent from the server, and then evaluated on the client.
    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)

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291
    Quote Originally Posted by Atheist
    So you're saying that there will only be 1 client application that will be used by many users? If so:
    Every message that the server sends should consist of atleast two parts: The first part is the 'Keyword' (defines what the message actually is), and the second part should be to which user the message is addressed. Something like this:

    VB Code:
    1. "Message|Mr Bean|Go to bed!"

    Could be sent from the server, and then evaluated on the client.
    Ah! Thank you so much !

  25. #25

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Mathiaslylo
    Ah! Thank you so much !
    So what about a TCP client then?

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

    Re: [2005] TCP socket server example?

    What about it?
    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)

  27. #27

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    What about it?
    Well, I actually need an example of that as well

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

    Re: [2005] TCP socket server example?

    Post #13 is all about the clients communication
    The only thing I havent shown is how to send data from the client to the server, but it is done exactly like on the server side. Write to the stream using a streamwriter:

    VB Code:
    1. Public Sub SendData(ByVal data As String)
    2.             Try
    3.                 Dim writer As New IO.StreamWriter(client.GetStream)
    4.                 writer.Write(data)
    5.                 writer.Flush()
    6.             Catch ex As Exception
    7.                 MessageBox.Show("An error occured, are you sure the server is running?.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    8.             End Try
    9.     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)

  29. #29

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    Post #13 is all about the clients communication
    The only thing I havent shown is how to send data from the client to the server, but it is done exactly like on the server side. Write to the stream using a streamwriter:

    VB Code:
    1. Public Sub SendData(ByVal data As String)
    2.             Try
    3.                 Dim writer As New IO.StreamWriter(client.GetStream)
    4.                 writer.Write(data)
    5.                 writer.Flush()
    6.             Catch ex As Exception
    7.                 MessageBox.Show("An error occured, are you sure the server is running?.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    8.             End Try
    9.     End Sub
    You're right, but post #13 doesn't show how to connect with a client

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

    Re: [2005] TCP socket server example?

    ah.
    You connect the client to the server in one of two ways, either you use the Connect method, or you specify a host and a port when you instanciate it:

    VB Code:
    1. Dim Client As New System.Net.Sockets.TcpClient
    2. Client.Connect("127.0.0.1", 30320)
    or
    VB Code:
    1. Dim Client As New System.Net.Sockets.TcpClient("127.0.0.1", 30320)
    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)

  31. #31

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    ah.
    You connect the client to the server in one of two ways, either you use the Connect method, or you specify a host and a port when you instanciate it:

    VB Code:
    1. Dim Client As New System.Net.Sockets.TcpClient
    2. Client.Connect("127.0.0.1", 30320)
    or
    VB Code:
    1. Dim Client As New System.Net.Sockets.TcpClient("127.0.0.1", 30320)
    Now, I bet this is not ascynchronous? There will be a complete freeze, until it connects. Right?

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

    Re: [2005] TCP socket server example?

    It will pause the application for a very short while, and if no connection could be established, it will throw an exception.
    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)

  33. #33

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    It will pause the application for a very short while, and if no connection could be established, it will throw an exception.
    One problem. The codes you sent for sending data from the server to a client, does not work.

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

    Re: [2005] TCP socket server example?

    Whats happening? Have you tried stepping through the code?
    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)

  35. #35

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

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    Whats happening? Have you tried stepping through the code?
    Well, I re-modified your code a bit, and here's what I've got, which doesn't work. It does not arrive at the client.

    Private Function Send(ByVal user As Long, ByVal data As String) As Boolean
    Debug.Print("Sending")
    If Clients.ContainsKey(user) = True Then
    Debug.Print("User has been found")
    Dim sw As System.Net.Sockets.NetworkStream = Clients.Item(user).GetStream
    Debug.Print("Stream writer created and write property is " & sw.CanWrite.ToString.ToLower)
    Dim d() As Byte
    d = System.Text.Encoding.ASCII.GetBytes(data)
    sw.Write(d, 0, d.Length)
    Debug.Print("Sent")
    Return True
    Else
    Return False
    End If
    End Function

  36. #36

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

    Re: [2005] TCP socket server example?

    Alright, I fixed that. Now, the error is inside my DoListen sub that I also modified from your own. Look at this.

    Code:
        Public Function FindUsedIndexes() As String()
            Dim Index1 As Long
            Dim Temp As String = "0"
            Debug.Print("Used indexes from 1 to " & Clients.Values.Count)
            For Index1 = 1 To Clients.Values.Count
                On Error Resume Next
                If Not Clients.Item(Index1) Is Nothing Then
                    Temp = Temp & "#" & Index1
                End If
            Next
            Debug.Print("Used indexes was " & Temp & "#0")
            Return Split(Temp & "#0", "#")
        End Function
        Public Function FindFreeIndex() As Long
            Dim Index1 As Long
            For Index1 = 1 To 1000
                GoTo StartLoop
    NextIndex:
                Return Index1
    StartLoop:
                On Error GoTo NextIndex
                If Clients.Item(Index1) Is Nothing Then
                    FindFreeIndex = Index1
                End If
            Next
        End Function
    
        Private Sub DoListen()
            Dim sr As IO.StreamReader
            Debug.Print("Going to listen!")
            Do
                Try
                    Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
                    If client.Connected = True Then
                        Debug.Print("A connection has just been accepted")
                        Ccount = FindFreeIndex()
                        Debug.Print("Free index found " & Ccount)
                        Clients.Add(Ccount, client)
                        Debug.Print("Accepted client added")
                        Dim UserList As String = "-None"
                        Dim ChoosenIndex As Integer = -1
                        Dim Index As Long
                        Debug.Print("Entering data collection loop")
                        For Index = 1 To 100
                            If Index = Ccount Then
                                User(Index).Available = 1
                                User(Index).RequestID = Ccount
                                Exit For
                            Else
                                UserList = UserList & "-" & User(Index).Name
                            End If
                        Next
                        Send(Ccount, "A" & Ccount & "-" & Mid(UserList, 2))
                    End If
                    For Each Cc As String In FindUsedIndexes()
                        If Not CLng(Cc) = 0 Then
                            Debug.Print("Creating stream reader " & Cc)
                            sr = New IO.StreamReader(Clients.Item(CLng(Cc)).GetStream) 'This line returns a System.InvalidOperationException
                            Debug.Print("Reading to end")
                            Dim dat As String = sr.ReadToEnd
                            Debug.Print("Splitting data")
                            Dim Data() As String = Split(dat, "|")
                            Debug.Print("It sent the data " & dat)
                            sr.Close()
                            Received(Data(1), Data(0))
                        End If
                    Next
                Catch
                End Try
            Loop
        End Sub
    
        Private Function Send(ByVal user As Long, ByVal data As String) As Boolean
            Debug.Print("Sending")
            If Clients.ContainsKey(user) = True Then
                Debug.Print("User has been found")
                Dim sw As New IO.StreamWriter(Clients.Item(user).GetStream)
                sw.Write(data)
                sw.Flush()
                sw.Close()
                Debug.Print("Sent")
                Return True
            Else
                Return False
            End If
        End Function

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

    Re: [2005] TCP socket server example?

    First off, before I even look at the code more. I just want to say that GoTo and On Error statements should not be used. If you ever end up "needing" to use them, you know youve got a bad code design. And On Error Resume Next is defenitly not recommended as it does not "handle" any errors, it just ignores them.
    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)

  38. #38

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

    Re: [2005] TCP socket server example?

    I found the solution for that problem, and I am now trying to create a UDP client as well, by the codes you made for the TCP client. However, it can't receive data. Please let me know what I am doing wrong here:

    Code:
            Public Sub Connect(ByVal HostName As String, ByVal Port As Integer)
                If Port = 0 Then
                    Err.Raise(44, "MathyProductions.Sockets.TCP.Client", "The port to connect to was not set")
                End If
                If HostName = vbNullString Then
                    Err.Raise(45, "MathyProductions.Sockets.TCP.Client", "The hostname to connect to was not set")
                End If
                Client.Connect(HostName, Port)
                Client.BeginReceive(AddressOf DoRead, Nothing)
            End Sub
            Private Sub DoRead(ByVal ar As IAsyncResult)
                Try
                    MsgBox("Read something?")
                    Dim EndPoint As System.Net.IPEndPoint = Nothing
                    Dim Bytes As [Byte]()
                    Bytes = Client.EndReceive(ar, EndPoint)
                    If Not Bytes Is Nothing Then
                        Dim Data As String = System.Text.Encoding.ASCII.GetString(Bytes)
                        If Mid(Data, 1, 2) = "ID" Then
                            ClientIndex = CLng(Mid(Data, 3, 1))
                            If Len(Mid(Data, 4)) > 0 Then
                                RaiseEvent ClientReceived(Mid(Data, 4))
                            Else
                                RaiseEvent ServerConnected()
                            End If
                        Else
                            RaiseEvent ClientReceived(Data)
                        End If
                    Else
                        Debug.Print("MathyProductions.Sockets.UDP.Client: Could not download data")
                    End If
                    Client.BeginReceive(AddressOf DoRead, Nothing)
                Catch e As Exception
                    RaiseEvent ClientError(Err.Number, e.Message)
                End Try
            End Sub

  39. #39

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

    Re: [2005] TCP socket server example?

    Anyone?

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

    Re: [2005] TCP socket server example?

    You have mixed the code with alot of old VB6 code. You shouldnt do that in VB .Net.
    Besides, why have you changed so much of the original code?
    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)

Page 1 of 2 12 LastLast

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