Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a two-player Quiz bee game LAN-Based. The players takes turn one at a time. A player will click a button to pick a question. On the first two turns, sending and receiving of data are working but on the third turn the other player didn't receive the incoming data.

Anyone can help me to avoid this problem?


What I have tried:

VB
**Variables for `UDPClient`**

    Public Module Server
        Public publisher As New Sockets.UdpClient(0)
        Public subscriber As New Sockets.UdpClient(54545)
    End Module

**Here is my code for TheGame.vb (Receiving data)**

    Private Sub tmrUDP_Tick(sender As Object, e As EventArgs) Handles tmrUDP.Tick
        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
            Dim receiveBytes() As Byte = subscriber.Receive(ep)
            lblReceive.Text = ASCII.GetString(receiveBytes)

            If lblReceive.Text = "b1" Then
                bttn1.BackgroundImage = My.Resources.Bee_Button_Blue
                btn1 = False
                bluebtn1 = True
                UncheckRB()
                bttn1.Enabled = False
                bttn1.Visible = False
                pcbxb1.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused1 = True

                publisher.Close()
                subscriber.Close()
                '=====================
            ElseIf lblReceive.Text = "b2" Then
                bttn2.BackgroundImage = My.Resources.Bee_Button_Blue

                btn2 = False
                bluebtn2 = True
                UncheckRB()
                bttn2.Enabled = False
                bttn2.Visible = False
                pcbxb2.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused2 = True
                '=====================

    'same code until "b25"

    ElseIf lblReceive.Text = "r1" Then
                bttn1.BackgroundImage = My.Resources.Bee_Button_RED

                btn1 = False
                redbtn1 = True
                UncheckRB()
                bttn1.Enabled = False
                bttn1.Visible = False
                pcbxr1.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused1 = True
                '=====================
            ElseIf lblReceive.Text = "r2" Then
                bttn2.BackgroundImage = My.Resources.Bee_Button_RED

                btn2 = False
                redbtn2 = True
                UncheckRB()
                bttn2.Enabled = False
                bttn2.Visible = False
                pcbxr2.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused2 = True
                '=====================
    'same code until r25
    End Sub

**TheGame_Load**

    Private Sub TheGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    subscriber.Client.ReceiveTimeout = 100
        subscriber.Client.Blocking = False
    End Sub

**Here is the Sending Data event**(*button event from another `Form` to submit answer*)

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If btn1 = True And TheGame.lblColor.Text = "Blue" Then

                            publisher.Connect(TheGame.lblIPother.Text, TheGame.lblPort.Text)
                            Dim sendByte() As Byte = ASCII.GetBytes("b1")
                            publisher.Send(sendByte, sendByte.Length)

                            AddScore()
                            TheGame.bttn1.BackgroundImage = My.Resources.Bee_Button_Blue
                            Me.Hide()
                            btn1 = False
                            bluebtn1 = True
                            UncheckRB()
                            TheGame.bttn1.Enabled = False
                            btnused1 = True
                            disableButtons()
                            ShowHideColorPanel()

    ElseIf btn1 = True And TheGame.lblColor.Text = "Red" Then

                            publisher.Connect(TheGame.lblIPother.Text, TheGame.lblPort.Text)
                            Dim sendByte() As Byte = ASCII.GetBytes("r1")
                            publisher.Send(sendByte, sendByte.Length)
                            AddScore()
                            TheGame.bttn1.BackgroundImage = My.Resources.Bee_Button_RED
                            Me.Hide()

                            btn1 = False
                            redbtn1 = True
                            UncheckRB()
                            TheGame.bttn1.Enabled = False
                            btnused1 = True
                            disableButtons()
                            ShowHideColorPanel()
    'same code until `btn25`
    End Sub
Posted
Updated 25-Feb-17 16:20pm
v4
Comments
[no name] 25-Feb-17 22:20pm    
"the other player didn't receive the incoming data", is probably because you are using "UdpClient".
DiamondKid 25-Feb-17 23:18pm    
What should i use?
[no name] 26-Feb-17 10:13am    
*I* would use a protocol that ensures delivery of data packets myself instead of waiting on a protocol that drops packets on a whim.

1 solution

Apps that appear "locked up" is usually an indication that a long running task is active on the UI thread blocking the UI message pipe. If you are indeed using Sam's code, then the UDP tasks are on a sperate thread.

To Debug this, when there is a long pause/"app appears locked" event, hit the VS pause button and see when the app is busy. If you don't land on code keep stepping until you do. You will see exactly where one of your problem(s) are.
 
Share this answer
 
Comments
DiamondKid 25-Feb-17 22:32pm    
I did what you said and it directs me to this code

Private Sub tmrUDP_Tick(sender As Object, e As EventArgs) Handles tmrUDP.Tick
        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
            Dim receiveBytes() As Byte = subscriber.Receive(ep)
            lblColor.Text = ASCII.GetString(receiveBytes)

            If lblColor.Text = "b" Then
                btnBlue.Visible = False
            ElseIf lblColor.Text = "r" Then
                btnRed.Visible = False
            End If

        Catch ex As Exception

        End Try
    End Sub


The above code is for selecting player's color(blue or red). If a player chose Blue, the Button Blue will disappear on the other player's windows form.
Graeme_Grant 25-Feb-17 22:43pm    
Using a timer to "look" rather than a receiver thread to "listen" is a big mistake. You will experience packet data loss. The other issue is that the inbound data might be out of step to what you are currently expecting.

This is not how Sam's example works. The receiver should not care what data is inbound, but simply pass it off to state logic to decide how it should be processed.

Also, do you understand how UDP works? User Datagram Protocol - Wikipedia[^]
DiamondKid 25-Feb-17 23:26pm    
I used that timer from LAN Chat using UDPClient tutorial. I tried Sam's code, but where does the message goes?
Graeme_Grant 26-Feb-17 0:08am    
You have to write the app UPD message state machine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900