Results 1 to 15 of 15

Thread: [Resolved] UDP Client/Server Help Needed!

  1. #1

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    [Resolved] UDP Client/Server Help Needed!

    I am very new to VB.net 2008, and i need to make a program where The client sends a number to the server, which notifies the client that it recieved that specific number, which then causes the client to automatically send a new number and wait for the notification before it sends again....

    If anybody can help out, it would be greatly appreciated. Coming from VB 6.0 i am getting very confused with the way it is done in .NET.

    This is my Current Code:

    Server:

    Imports System Code:
    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Text
    5.  
    6. Public Class Server
    7.     Public ipAddr As System.Net.IPAddress
    8.  
    9.  
    10.     Private Sub receiver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    11.  Handles MyBase.Load
    12.         Dim t As New Threading.Thread(AddressOf listen)
    13.         t.IsBackground = True
    14.         t.Start()
    15.     End Sub
    16.  
    17.     Private Sub receiver_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    18.     Handles Me.FormClosing
    19.         If udp IsNot Nothing Then udp.Close()
    20.     End Sub
    21.  
    22.     Private udp As Net.Sockets.UdpClient
    23.     Private sendMessage As Net.Sockets.UdpClient
    24.     Private Sub listen()
    25.         Try
    26.             udp = New Net.Sockets.UdpClient(6100)
    27.  
    28.             sendMessage = New Net.Sockets.UdpClient(6103)
    29.  
    30.             udp.EnableBroadcast = True
    31.  
    32.             Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
    33.             Do
    34.  
    35.                 Dim b() As Byte = udp.Receive(ep)
    36.                 Me.Invoke(safeAddText, System.Text.Encoding.UTF32.GetString(b))
    37.  
    38.  
    39.             Loop
    40.         Catch ex As Exception
    41.             MsgBox(ex.Message)
    42.  
    43.             ' If udp IsNot Nothing Then udp.Close()
    44.         End Try
    45.     End Sub
    46.  
    47.     Private Delegate Sub delAddText(ByVal text As String)
    48.  
    49.     Private safeAddText As New delAddText(AddressOf AddText)
    50.  
    51.     Private Sub AddText(ByVal text As String)
    52.         ListBox1.Items.Add(text)
    53.         Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
    54.         Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(text)
    55.         sendMessage.Send(b, b.Length, ep)
    56.  
    57.     End Sub
    58.  
    59. End Class

    Client:

    Imports System.Net Code:
    1. Imports System.Net
    2. Imports System.Net.Sockets
    3. Imports System.Text
    4. Imports Microsoft.VisualBasic.Strings
    5.  
    6. Public Class Frmmain
    7.     Public Const Payload As String = "OCX000000FFFFFF"
    8.     Public Counter As Integer = 1
    9.     Public LastString As String
    10.  
    11.  
    12.     Private Sub Frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.         Dim t As New Threading.Thread(AddressOf listen)
    14.         t.IsBackground = True
    15.         t.Start()
    16.     End Sub
    17.     Private udp As Net.Sockets.UdpClient
    18.     Private sendMessage As Net.Sockets.UdpClient
    19.  
    20.     Private Sub listen()
    21.  
    22.         Try
    23.             udp = New Net.Sockets.UdpClient(6101)
    24.  
    25.             udp.EnableBroadcast = True
    26.  
    27.             Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
    28.             Do
    29.  
    30.                 Dim b() As Byte = udp.Receive(ep)
    31.                 Me.Invoke(safeAddText, System.Text.Encoding.UTF32.GetString(b))
    32.  
    33.             Loop
    34.  
    35.         Catch ex As Exception
    36.             MsgBox("here:" & ex.Message)
    37.  
    38.             ' If udp IsNot Nothing Then udp.Close()
    39.         End Try
    40.     End Sub
    41.  
    42.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    43. Handles Button1.Click
    44.  
    45.         Dim udp As New Net.Sockets.UdpClient()
    46.         udp.EnableBroadcast = True
    47.         Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
    48.         Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Recieved")
    49.         udp.Send(b, b.Length, ep)
    50.  
    51.     End Sub
    52.  
    53.     Private Delegate Sub delAddText(ByVal text As String)
    54.     Private safeAddText As New delAddText(AddressOf AddText)
    55.  
    56.     Private Sub AddText(ByVal text As String)
    57.         If (text.ToString = LastString.ToString) Then
    58.             Counter += 1
    59.         End If
    60.         Listbox1.Items.Add(text)
    61.         Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
    62.         Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(text)
    63.         '  sendMessage.Send(b, b.Length, ep)
    64.  
    65.     End Sub
    66.  
    67.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    68.  
    69.         Dim udp As New Net.Sockets.UdpClient()
    70.         udp.EnableBroadcast = True
    71.         Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
    72.         LastString = Counter.ToString.PadRight(4, "X") & Payload
    73.         Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(LastString)
    74.  
    75.         udp.Send(b, b.Length, ep)
    76.  
    77.     End Sub
    78. End Class

    I would like the client to send packets in a sequence number that increases, ONLY increases when it gets the acknowledgement from the server that it recieved the sequence that the client sent. The payload attached never changes. If the server has not responded in 2 seconds that it recieved the sent sequence, it acknowledges the last sequence it received telling the client to send the same sequence number as it did before. And if the first one doesnt make it, the server acknowledges 0. I hope thats clear enough...

    I also want the client to count how many times it needed to resend a packet twice or more before it got the acknowledgement from the server.

    I think thats really all i need to change, and change the server from using a manual button and text from a textbox, to what i mentioned above.. If you can help me get these last bugs fixed i think ill be finished!

    Thanks so much!
    Last edited by Monkz; Aug 12th, 2009 at 09:04 AM.

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

    Re: UDP Client/Server Help Needed!

    I'm not completely following, if the server did not receive the sequence number and thus does not send an acknowledgement, the client will wait for 2 seconds until acknowledges the last acknowledged packet? So the acknowledgement goes both ways?
    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 Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    The client starts at sequence # 1.

    The server starts expecting sequence #1 and believing it already had Sequence # 0..

    Therefore if the client sends a packet and the server doesnt recieve it or receives the wrong sequence number it will acknowledge that it recieved Sequence 0, so then the client sends sequence 1 again, and marks that it lost a packet because it sent 1 a second time. If the server receives it, then it acknowledges that it recieved the sequence number 1, so the client sends sequence number 2. All the sequences have the same payload attached.

    So really the server is checking to make sure the payload isnt mutated either.

  4. #4

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Right now in my code, its set up that the client just sends the next sequence number in line without requireing an acknowledgement.. I want to change it to work as i mentioned above.. Im also experienceing issues with:

    Code:
            Dim udp As New Net.Sockets.UdpClient()
            udp.EnableBroadcast = True
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Recieved")
            udp.Send(b, b.Length, ep)
    In the server under Button1_Click. I dont want to use a button, i want the server to send back to the client automatically.

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

    Re: UDP Client/Server Help Needed!

    So the server would be the one that needs the 2 second timeout.
    Well, start off on the client side by sending the first sequence number, then immediately call the Receive method to wait for any incoming acknowledgements. When one arrives, you'd need to check its sequence number and send a new datagram to the server with (acked sequence number + 1) as sequence number. Repeat the entire process.

    On the server side, you'd just call Receive to sit and wait for incoming datagrams. When one arrives you just send an acknowledgement. You'll need to set the ReceiveTimeout property on the underlying socket to 2000 aswell. The Receive method will throw an exception when the timeout has been reached, at which point you'd need to send an acknowledgement for the sequence number (expected sequence number - 1).

    If you go from the top to bottom, what part are you getting stuck on?
    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 Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Well im thinking.. The server doesnt need it really.. If the packet gets lost from the client, lets just have the client send a new packet on its 2 second timer, as it will still not have the acknowledgement to send the next sequence number.

  7. #7

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Right now the issue i was going to strike at first was to get this:

    Code:
            Dim udp As New Net.Sockets.UdpClient()
            udp.EnableBroadcast = True
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Recieved")
            udp.Send(b, b.Length, ep)
    Midway in serverside under Button1_Click

    To automatically send the recieved message upon receiving instead of having to click the button.

    After this, i was going to work on setting the requirement for the client to wait for the acknowledgements.

  8. #8

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Also whenever the sequence gets to 10, it starts sending 10XXOCX000000FFFFFF instead of 10XXXOCX000000FFFFFF

  9. #9

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    I had noticed i had labelled the client and server wrongly earlier so i edited my thing up there.

  10. #10

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Ok i am testing my thing, and i modified some of my code, and i think i have my client and server offset.. I think they are both sending the other one stuff independantly. Instead of the server waiting for the clients packet, then sending back. All im trying to do at this point is Get the client to send, server to send back an acknowledgement with the sequence number it received, and have the client send a new packet.

    Right now i think the client and server are sending to eachother simultaneously.

  11. #11

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    When i change this server code to that change in bold, the client says Sent Blue [1XXXOCX000000FFFFFF]. I would believe if the client was sending first it would not put sent blue in its textbox.

    Code:
        Private Sub AddText(ByVal text As String)
            ListBox1.Items.Add("Received " & text)
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Blue" & text)
            sendMessage.Send(b, b.Length, ep)
    
        End Sub
    Could u please help me find where im going wrong? :S

  12. #12

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    When i do:

    On Client:

    Code:
          Private Sub AddText(ByVal text As String)
            If (text.ToString = LastString.ToString) Then
                Sequence += 1
            End If
            Listbox1.Items.Add("Sent " & text)
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(text)
            'sendMessage.Send(b, b.Length, ep)
    
        End Sub
    
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim udp As New Net.Sockets.UdpClient()
            udp.EnableBroadcast = True
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
            LastString = Counter.ToString.PadRight(4, "X") & Payload
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Happy" & LastString)
    
            udp.Send(b, b.Length, ep)
    
        End Sub
    And on Server:

    Code:
        Private Sub AddText(ByVal text As String)
            ListBox1.Items.Add("Received " & text)
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Blue" & text)
            sendMessage.Send(b, b.Length, ep)

    I get: Sent BlueHappy1XXXOCX000000FFFFFF On the client, and i get: Received Happy 1XXXOCX000000FFFFFF On the server.

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

    Re: UDP Client/Server Help Needed!

    All your posts are really making me confused. Can we take one thing at a time please?

    Lets begin with this:

    Right now the issue i was going to strike at first was to get this:


    [CODE=Monkz] Dim udp As New Net.Sockets.UdpClient()
    udp.EnableBroadcast = True
    Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6100)
    Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes("Recieved")
    udp.Send(b, b.Length, ep)[/CODE]
    Midway in serverside under Button1_Click

    To automatically send the recieved message upon receiving instead of having to click the button.

    After this, i was going to work on setting the requirement for the client to wait for the acknowledgements.
    What is the actual problem? Have you made an attempt at executing this code when something is received? If so, where did it go wrong?
    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 Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    Ok that code works, but im just trying to use it somewhere in the server, which triggers as soon as it recieves a packet.. So the client Should have:

    Sent 1OCX000000FFFFFF
    Server Received 1OCX000000FFFFFF
    Sent 2OCX000000FFFFFF
    Server Received 2OCX000000FFFFFF

    Instead of:

    Sent 1OCX000000FFFFFF
    Sent 2OCX000000FFFFFF

    Right now it takes a message from a textbox, and sends upon user clicking. Instead of on a click, i want it to work after the server finishes receiving the packet.

    So Far, i keep ruining the project whenever i try to move it
    Last edited by Monkz; Aug 12th, 2009 at 11:01 AM.

  15. #15

    Thread Starter
    Hyperactive Member Monkz's Avatar
    Join Date
    Aug 2009
    Posts
    304

    Re: UDP Client/Server Help Needed!

    I think this is a legitimate way to solve my problem.

    Code:
        Private Sub AddText(ByVal text As String)
    
            If (text.ToString = LastString.ToString) Then
                Sequence += 1
            Else
                packetloss += 1
            End If
            Listbox1.Items.Add("Sent " & LastString)
            Dim ep As New Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), 6101)
            Dim b() As Byte = System.Text.Encoding.UTF32.GetBytes(text)
            Listbox1.Items.Add("Server Received " & text.ToString)
            'sendMessage.Send(b, b.Length, ep)
    
        End Sub
    This code is on client side and is after the code checks if the recieved string from the server is the same as the sent string.
    Last edited by Monkz; Aug 12th, 2009 at 01:07 PM.

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