Results 1 to 22 of 22

Thread: [Resolved] Button works once, then stops working.

  1. #1

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

    [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:
    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    Read my last reply in your other thread.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

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

    Re: Button works once, then stops working.

    I believe i sent u a Private Message stating that i believed it had to be something to do with ReceivePackets(). I dont know where to move it, or how to change it without messing things up.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    OK... Here is the revised code to work with a timer:

    Form1 has 2 buttons and 1 timer. I used a random object to generate the values for the packets. Timer is set to 1 second interval.
    Code:
    Imports System.Net.Sockets
    
    Public Class Form1
    
        'Use a random object to generate random packets
        Private rand As New Random()
        Private udp As UdpClient = Nothing
        Private bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            udp = New UdpClient()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
        End Sub
    
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Dim frm2 As New Form2
            frm2.Show()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            SendPacket()
        End Sub
    
        Private Sub SendPacket()
            Dim sequence As UInt64 = CType(rand.Next(0, Integer.MaxValue), UInt64)
            Dim payload(32) As Byte
            For i As Integer = 0 To payload.GetUpperBound(0)
                payload(i) = CByte(rand.Next(0, 255))
            Next
            Dim pak As New Packet(sequence, payload)
            Using strem As New IO.MemoryStream()
                bf.Serialize(strem, pak)
                Dim stremByte() As Byte = strem.ToArray()
                udp.Connect("127.0.0.1", 5000)
                udp.Send(stremByte, stremByte.Length)
            End Using
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Timer1.Stop()
        End Sub
    End Class
    Nothing changed in Form2 except the ReceivePackets sub. Note that all I did was to move the client.BeginReceive line inside the loop.
    Code:
    Private Sub ReveivePackets()
            Dim state As New UdpState(client, endPoint)
            While True
                client.BeginReceive(New AsyncCallback(AddressOf ReceiveCallback), state)
                Application.DoEvents()
                System.Threading.Thread.Sleep(100)
            End While
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

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

    Re: Button works once, then stops working.

    Thanks so much! This is the sort of thing i was trying to do! Im going to use the same idea, but i am going to not use the buttons.. Ill have the timer enabled on startup.
    Thanks again!

  6. #6

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

    Re: Button works once, then stops working.

    I changed it from 264 bit payload to 256 bit. How do i get the random payload thing to work with the change?

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    264 bits = 33 bytes (1 byte = 8 bits, remember?). So 256 bits = (256\8) bytes = 32 bytes. All you have to do is to change the payload size to 32 byte array, which is set in the Payload property of the Packet class. In that property, the setter is tests the size of the array to make sure it's of a certain size, and if it isn't, an exception is thrown.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

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

    Re: Button works once, then stops working.

    I had done that already. But when i use the random payload code you supplied i get:

    "Payload must be a byte array of 32 element (32 bytes = 256 bits)"

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    This is the current code of the Payload Property
    Code:
    Public Property Payload() As Byte()
            Get
                Return _Payload
            End Get
            Set(ByVal value As Byte())
                If value.Length = 33 Then
                    _Payload = value
                Else
                    Throw New ArgumentException("Payload must be a byte array of 33 element (33 bytes = 264 bits)")
                End If
            End Set
        End Property
    Do you see the number 33 in red? That needs to be changed to 32 if you want the payload to be 256 bits.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

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

    Re: Button works once, then stops working.

    This what i have:

    Code:
     Public Property Payload() As Byte()
            Get
                Return _Payload
            End Get
            Set(ByVal value As Byte())
                If value.Length = 32 Then
                    _Payload = value
                Else
                    Throw New ArgumentException("Payload must be a byte array of 32 element (32 bytes = 256 bits)")
                End If
            End Set
        End Property
    I did this earlier.

  11. #11

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

    Re: Button works once, then stops working.

    It works before i change to the random code you supplied earlier, as long as i dont go over 32.

    But i want to get what we have as 1 - 32 to be composed of random hexadecimal numbers from 1-9, A - F.


    So Eg.

    Instead of being in order from 1 - 32 as the very original code was, i might get randomly each time:

    F, G, A, 1, 7 , 3, B etc... till im at 256 bits..

    So i thought i could get that random code u use working, and then build onto it to make it work that way i just explained.. Just so far im getting this error.
    Last edited by Monkz; Aug 18th, 2009 at 09:48 AM.

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    1. Hexadecimal notation is only for displaying purpose.
    2. 1 hex digit = 4 bits
    3. So 1 byte = 8 bits = 2 hex digits.
    4. You have an array of 32 bytes, and you want to populate that with random values, you just need to generate 32 random bytes and assign them to your array.
    5. If you want to see the value of a byte in hex notation, you call Tostring("X") on that byte... Note that the leading zero will be dropped. So if you want to keep the leading zero, you have to add it yourself.
    Try this:
    Code:
            Dim b As Byte = 15
            Dim hex As String = b.ToString("X")
            MessageBox.Show(hex)
            Dim hexWithLeadingZero As String = b.ToString("X")
            If hex.Length < 2 Then
                hexWithLeadingZero = "0" & hex
            End If
            MessageBox.Show(hexWithLeadingZero)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  13. #13

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

    Re: Button works once, then stops working.

    So on a new form application i came up with this:

    vb Code:
    1. Private Rand As New Random
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim b As Byte = CType(Rand.Next(1, 15), Byte)
    4.         Dim hex As String = b.ToString("X")
    5.         Dim hexWithLeadingZero As String = b.ToString("X")
    6.         If hex.Length < 2 Then
    7.             hexWithLeadingZero = "0" & hex
    8.         End If
    9.         MessageBox.Show(hexWithLeadingZero)
    10.     End Sub

    I believe this is close to what i need to achieve on my payload.. But i dont know how to apply this to all 32.

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    As I repeatedly say, 1 byte = 8 bits, and it can hold a value between 0-255 (inclusive).
    So when you want to generate a random byte, you want to generate a value between 0 and 255, not between 1 and 14 as your code shows.
    Code:
    Dim b As Byte = CType(Rand.Next(0, 256), Byte)
    That line of code generate 1 random byte. To generate 32 random bytes, you just need a loop of 32 iterations that executes that line in each iteration. Once you have a random byte, what you want to do with it (stick it in an array, for example) is up to you.
    Last edited by stanav; Aug 18th, 2009 at 11:19 AM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  15. #15

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

    Re: Button works once, then stops working.

    should i also use Randomize for this?

  16. #16
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    Quote Originally Posted by Monkz View Post
    should i also use Randomize for this?
    No... Since you're coding in vb.net, you should stick with .net classes as much as possible. Use the System.Random class.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  17. #17

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

    Re: Button works once, then stops working.

    So what approach would you suggest i do to get this random hexadecimal code to work in the project?

    I tried to put it in, but when it converts to byte it says something like cant convert byte to 1-dimensional array of byte. On the line which makes it part of the packet.

  18. #18

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

    Re: Button works once, then stops working.

    This is what i currently am using:

    vb Code:
    1. Private Sub Acknowledge()
    2.  
    3.         Dim x As Byte = CType(rand.Next(0, 256), Byte)
    4.         Dim hex As String = x.ToString("X")
    5.  
    6.         If hex.Length < 2 Then
    7.             hex = "0" & hex
    8.         End If
    9.  
    10.         Dim payload2() As Byte = System.Text.Encoding.UTF32.GetBytes(hex)
    11.  
    12.         Dim pak As New Packet(sequence, payload2)
    13.         Dim strem As New IO.MemoryStream()
    14.  
    15.         bf.Serialize(strem, pak)
    16.         Dim stremByte() As Byte = strem.ToArray()
    17.         udp.Connect("127.0.0.1", 6000)
    18.         udp.Send(stremByte, stremByte.Length)
    19.         strem.Close()
    20.  
    21.     End Sub

    I bet its because i dont have it looping the hex yet, but i dont know how im going to do that and get all 32.

  19. #19
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    I don't know what to say... Can you go back and read post#12, item #1.
    You don't send hex string in your packet. You just send a byte array. If you are curious and want to see what that byte array looks like in hex notation, you loop thru the array and acll tostring("x") to each byte, append it to an output string and display it in your UI. You do the same at the receiving end. But the data you send is still the byte array. You don't convert it to hex, because the hex string is just another representation of the actual value.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  20. #20

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

    Re: Button works once, then stops working.

    So what im understanding is:

    Code:
    Dim Payload As Byte = CType(Rand.Next(0, 256), Byte)
    And then the rest of the hex conversion i do to display directly to my UI?


    Its the looping part that keeps confusing me though.. I can never get them to work..

    Would i do somethine like Do Until intloop = 32, and each time at the end of the loop, i make intloop += 1?

  21. #21
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Button works once, then stops working.

    Code:
    'Declare a byte array with 32 element 
    Dim payload(31) as Byte
    'Now use a loop to generate random bytes to populate that array
    For i as Integer = 0 to 31
       payload(i) = CType(Rand.Next(0, 256), Byte)
    Next
    'Now you have a 32-byte array filled with random bytes.... Do what you want with it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  22. #22

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

    Re: Button works once, then stops working.

    Thanks man! I wanted the server to send back the same random payload it received for the client. I got stuck on this one for awhile and was about to make another post but i got it! Probably not the best way to do it, but it is working perfectly.

    The client sends the 32-byte array, which the server translates to its texbox in hexadecimal, and then resends the same array, which the client then translates to its textbox.. Upon testing they match!

    Thanks so much for the help! Ill mark this as resolved.. Thanks again! You are great!

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