Results 1 to 15 of 15

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

Threaded View

  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.

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