|
-
Aug 17th, 2009, 11:34 AM
#1
Thread Starter
Hyperactive Member
[Resolved] Button works once, then stops working.
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:
Option Strict Off
Imports System.Net.Sockets
Public Class Client
Dim sequence As UInt64
Private udp As UdpClient = Nothing
Private client As UdpClient = Nothing
Private packetReceived As Boolean = False
Private endPoint As Net.IPEndPoint = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sequence = 1
udp = New UdpClient()
endPoint = New Net.IPEndPoint(Net.IPAddress.Any, 6000)
client = New UdpClient(endPoint)
Label1.Text = sequence
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
ReceivePackets()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
Private Sub ReceivePackets()
Dim state As New UdpState(client, endPoint)
client.BeginReceive(New AsyncCallback(AddressOf ReceiveCallback), state)
While packetReceived = False
Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While
End Sub
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Dim state As UdpState = DirectCast(ar.AsyncState, UdpState)
Dim u As UdpClient = state.UdpClient
Dim e As Net.IPEndPoint = state.EndPoint
Dim receiveBytes As Byte() = u.EndReceive(ar, e)
Dim strem As New IO.MemoryStream(receiveBytes)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim obj As Object = bf.Deserialize(strem)
Dim pak As Packet = DirectCast(obj, Packet)
'Display the packet on UI
ShowPacketData(pak)
packetReceived = True
End Sub
Private Delegate Sub ShowPacketDataCallback(ByVal pak As Packet)
Private Sub ShowPacketData(ByVal pak As Packet)
If Me.InvokeRequired Then
sequence = pak.Sequence
Me.Invoke(New ShowPacketDataCallback(AddressOf ShowPacketData), pak)
Else
TextBox1.Text = ("Server Received Sequence: " & pak.Sequence.ToString - 1)
For Each b As Byte In pak.Payload
TextBox2.Text &= b.ToString & " "
Label1.Text = sequence
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim payload As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, _
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
31, 32, 33}
Dim pak As New Packet(sequence, payload)
Dim strem As New IO.MemoryStream()
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(strem, pak)
Dim stremByte() As Byte = strem.ToArray()
udp.Connect("127.0.0.1", 5000)
udp.Send(stremByte, stremByte.Length)
strem.Close()
End Sub
End Class
Heres my Server Code:
Option Strict Off Code:
Option Strict Off
Imports System.Net.Sockets
Public Class Server
Private client As UdpClient = Nothing
Private packetReceived As Boolean = False
Private endPoint As Net.IPEndPoint = Nothing
Dim sequence As UInt64
Private packetloss As Int64
Private udp As UdpClient = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sequence = 1
packetloss = 0
endPoint = New Net.IPEndPoint(Net.IPAddress.Any, 5000)
client = New UdpClient(endPoint)
udp = New UdpClient()
Label1.Text = sequence
End Sub
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
ReceivePackets()
End Sub
Private Sub ReceivePackets()
Dim state As New UdpState(client, endPoint)
client.BeginReceive(New AsyncCallback(AddressOf ReceiveCallback), state)
While packetReceived = False
Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While
End Sub
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Dim state As UdpState = DirectCast(ar.AsyncState, UdpState)
Dim u As UdpClient = state.UdpClient
Dim e As Net.IPEndPoint = state.EndPoint
Dim receiveBytes As Byte() = u.EndReceive(ar, e)
Dim strem As New IO.MemoryStream(receiveBytes)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim obj As Object = bf.Deserialize(strem)
Dim pak As Packet = DirectCast(obj, Packet)
'Display the packet on UI
ShowPacketData(pak)
packetReceived = True
End Sub
Private Delegate Sub ShowPacketDataCallback(ByVal pak As Packet)
Private Sub ShowPacketData(ByVal pak As Packet)
If sequence = pak.Sequence Then
sequence += 1
Else
packetloss += 1
End If
If Me.InvokeRequired Then
Me.Invoke(New ShowPacketDataCallback(AddressOf ShowPacketData), pak)
Else
TextBox1.Text = pak.Sequence.ToString
Label1.Text = sequence
For Each b As Byte In pak.Payload
TextBox2.Text &= b.ToString & " "
Next
End If
Acknowledge()
End Sub
Private Sub Acknowledge()
Dim payload2 As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, _
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
31, 32, 33}
Dim pak As New Packet(sequence, payload2)
Dim strem As New IO.MemoryStream()
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(strem, pak)
Dim stremByte() As Byte = strem.ToArray()
udp.Connect("127.0.0.1", 6000)
udp.Send(stremByte, stremByte.Length)
strem.Close()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|