I am having issues with my program. It should update every time i click the button, but it runs through perfectly on the first click, after the first click it does nothing.

This is a UDP Program, where the client sends a packet with a sequence number and payload which are separate items in the packet. The server receives the packet, reads the sequence number, and then sends back the next sequence number the client should send.

The client then receives that from the server with the same payload.. And then should change its sequence number to the one it recieved, and send that to the server..

The client works up to where the client updates to the sequence number it receives from the server.. But both show no effect on any clicks after.

Heres my Client Code:

Option Strict Off Code:
  1. Option Strict Off
  2. Imports System.Net.Sockets
  3.  
  4. Public Class Client
  5.     Dim sequence As UInt64
  6.     Private udp As UdpClient = Nothing
  7.     Private client As UdpClient = Nothing
  8.     Private packetReceived As Boolean = False
  9.     Private endPoint As Net.IPEndPoint = Nothing
  10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.         sequence = 1
  12.         udp = New UdpClient()
  13.         endPoint = New Net.IPEndPoint(Net.IPAddress.Any, 6000)
  14.         client = New UdpClient(endPoint)
  15.         Label1.Text = sequence
  16.  
  17.     End Sub
  18.     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
  19.         ReceivePackets()
  20.     End Sub
  21.  
  22.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  23.  
  24.     End Sub
  25.  
  26.     Private Sub ReceivePackets()
  27.         Dim state As New UdpState(client, endPoint)
  28.         client.BeginReceive(New AsyncCallback(AddressOf ReceiveCallback), state)
  29.         While packetReceived = False
  30.             Application.DoEvents()
  31.             System.Threading.Thread.Sleep(100)
  32.         End While
  33.     End Sub
  34.  
  35.     Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
  36.         Dim state As UdpState = DirectCast(ar.AsyncState, UdpState)
  37.         Dim u As UdpClient = state.UdpClient
  38.         Dim e As Net.IPEndPoint = state.EndPoint
  39.         Dim receiveBytes As Byte() = u.EndReceive(ar, e)
  40.         Dim strem As New IO.MemoryStream(receiveBytes)
  41.         Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  42.         Dim obj As Object = bf.Deserialize(strem)
  43.         Dim pak As Packet = DirectCast(obj, Packet)
  44.         'Display the packet on UI
  45.         ShowPacketData(pak)
  46.         packetReceived = True
  47.     End Sub
  48.  
  49.     Private Delegate Sub ShowPacketDataCallback(ByVal pak As Packet)
  50.     Private Sub ShowPacketData(ByVal pak As Packet)
  51.  
  52.         If Me.InvokeRequired Then
  53.             sequence = pak.Sequence
  54.             Me.Invoke(New ShowPacketDataCallback(AddressOf ShowPacketData), pak)
  55.         Else
  56.             TextBox1.Text = ("Server Received Sequence: " & pak.Sequence.ToString - 1)
  57.             For Each b As Byte In pak.Payload
  58.                 TextBox2.Text &= b.ToString & " "
  59.                 Label1.Text = sequence
  60.             Next
  61.         End If
  62.  
  63.     End Sub
  64.  
  65.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  66.  
  67.         Dim payload As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, _
  68.                              11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
  69.                              21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
  70.                              31, 32, 33}
  71.         Dim pak As New Packet(sequence, payload)
  72.         Dim strem As New IO.MemoryStream()
  73.         Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  74.         bf.Serialize(strem, pak)
  75.         Dim stremByte() As Byte = strem.ToArray()
  76.         udp.Connect("127.0.0.1", 5000)
  77.         udp.Send(stremByte, stremByte.Length)
  78.         strem.Close()
  79.     End Sub
  80. End Class

Heres my Server Code:

Option Strict Off Code:
  1. Option Strict Off
  2. Imports System.Net.Sockets
  3.  
  4. Public Class Server
  5.     Private client As UdpClient = Nothing
  6.     Private packetReceived As Boolean = False
  7.     Private endPoint As Net.IPEndPoint = Nothing
  8.     Dim sequence As UInt64
  9.     Private packetloss As Int64
  10.     Private udp As UdpClient = Nothing
  11.  
  12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13.         sequence = 1
  14.         packetloss = 0
  15.         endPoint = New Net.IPEndPoint(Net.IPAddress.Any, 5000)
  16.         client = New UdpClient(endPoint)
  17.         udp = New UdpClient()
  18.         Label1.Text = sequence
  19.     End Sub
  20.  
  21.     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
  22.         ReceivePackets()
  23.     End Sub
  24.  
  25.     Private Sub ReceivePackets()
  26.         Dim state As New UdpState(client, endPoint)
  27.         client.BeginReceive(New AsyncCallback(AddressOf ReceiveCallback), state)
  28.         While packetReceived = False
  29.             Application.DoEvents()
  30.             System.Threading.Thread.Sleep(100)
  31.         End While
  32.     End Sub
  33.  
  34.     Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
  35.         Dim state As UdpState = DirectCast(ar.AsyncState, UdpState)
  36.         Dim u As UdpClient = state.UdpClient
  37.         Dim e As Net.IPEndPoint = state.EndPoint
  38.         Dim receiveBytes As Byte() = u.EndReceive(ar, e)
  39.         Dim strem As New IO.MemoryStream(receiveBytes)
  40.         Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  41.         Dim obj As Object = bf.Deserialize(strem)
  42.         Dim pak As Packet = DirectCast(obj, Packet)
  43.         'Display the packet on UI
  44.         ShowPacketData(pak)
  45.         packetReceived = True
  46.     End Sub
  47.  
  48.     Private Delegate Sub ShowPacketDataCallback(ByVal pak As Packet)
  49.     Private Sub ShowPacketData(ByVal pak As Packet)
  50.  
  51.         If sequence = pak.Sequence Then
  52.             sequence += 1
  53.         Else
  54.             packetloss += 1
  55.         End If
  56.  
  57.  
  58.         If Me.InvokeRequired Then
  59.             Me.Invoke(New ShowPacketDataCallback(AddressOf ShowPacketData), pak)
  60.         Else
  61.             TextBox1.Text = pak.Sequence.ToString
  62.             Label1.Text = sequence
  63.             For Each b As Byte In pak.Payload
  64.                 TextBox2.Text &= b.ToString & " "
  65.             Next
  66.         End If
  67.  
  68.         Acknowledge()
  69.  
  70.     End Sub
  71.  
  72.     Private Sub Acknowledge()
  73.  
  74.         Dim payload2 As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, _
  75.                                             11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
  76.                                             21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
  77.                                             31, 32, 33}
  78.         Dim pak As New Packet(sequence, payload2)
  79.         Dim strem As New IO.MemoryStream()
  80.         Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  81.         bf.Serialize(strem, pak)
  82.         Dim stremByte() As Byte = strem.ToArray()
  83.         udp.Connect("127.0.0.1", 6000)
  84.         udp.Send(stremByte, stremByte.Length)
  85.         strem.Close()
  86.  
  87.     End Sub
  88. End Class