Как предотвратить замораживание формы (не удается получить входящие данные) - VB.NET спросите
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?
Что я уже пробовал:
**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
[no name]
"другой игрок не получил входящие данные", вероятно, потому, что вы используете"UdpClient".
DiamondKid
Что я должен использовать?
[no name]
Я бы использовал протокол, который сам обеспечивает доставку пакетов данных, вместо того чтобы ждать протокола, который отбрасывает пакеты по прихоти.