我想用tcp写一个多人游戏。发件人工作并发送数据,但在Receiver更改标签文本和PictureBox的位置后没有任何反应。我对它进行了调查,发现该标签有新的文本,但它没有在窗口中显示。 我google了一下,发现我的主要线程被阻止了什么。但是我可以移动第二个玩家。
Multiplayer.cs:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Game
{
class Multiplayer
{
public string ClientAdress;
public void StartSession(int port)
{
Form1 form1 = new Form1();
while (true)
{
try
{
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
Socket socket = listener.AcceptSocket();
byte[] b = new byte[100];
int k = socket.Receive(b);
string update = null;
for (int i = 0; i < k; i++)
{
update += Convert.ToChar(b[i]).ToString();
}
IPEndPoint remoteIPEndPoint = socket.RemoteEndPoint as IPEndPoint;
ClientAdress = remoteIPEndPoint.Address.ToString();
form1.GetData(ClientAdress, update);
socket.Close();
listener.Stop();
}
catch (Exception ex)
{
form1.Exception(ex);
}
}
}
public void JoinSession(string ipadress, int port)
{
Form1 form1 = new Form1();
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(ipadress, port);
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
string message = form1.SendData();
byte[] ba = asen.GetBytes(message);
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
tcpclnt.Close();
form1.senderRunning = false;
}
catch (Exception ex)
{
form1.Exception(ex);
form1.senderRunning = false;
}
}
}
}
}
将Button3:
Multiplayer mp = new Multiplayer();
private void button3_Click(object sender, EventArgs e)
{
if (!startjoinmenu)
{
Thread Listener = new Thread(delegate () { mp.StartSession(Int32.Parse(textBox2.Text)); });
if (!ThreadisRunning)
{
ThreadisRunning = true;
Listener.Start();
}
}
else
{
if (!senderRunning)
{
Thread Sender = new Thread(delegate () { mp.JoinSession(textBox1.Text, Int32.Parse(textBox2.Text)); });
senderRunning = true;
Sender.Start();
}
}
的GetData:
public void GetData(string client, string data)
{
try
{
string dataX;
string dataY;
int positionOf = data.IndexOf(";");
dataX = data.Substring(0, positionOf);
dataY = data.Substring(positionOf + 1, data.Length - positionOf - 1);
//enemyplayer.Location = new Point(Int32.Parse(dataX), Int32.Parse(dataY));
enemyplayer.Left = Int32.Parse(dataX);
enemyplayer.Top = Int32.Parse(dataY);
label3.Text = "Connected with: " + client + "!";
}
catch (Exception ex)
{
Exception(ex);
}
}
的SendData:
public string SendData()
{
string playerX = player.Location.X.ToString();
string playerY = player.Location.Y.ToString();
return playerX + ";" + playerY;
}
答案 0 :(得分:0)
我认为您在while(true)
方法中使用了StartSession
语句。这会导致主线程被阻塞,因为UI只在所有方法完成执行时更新(在这种情况下使用while (true)
它永远不会完成执行该方法。)
要解决此问题,您可以使用Application.DoEvents();
方法。您可以阅读有关此here的更多信息。这将告诉应用程序处理等待执行的其他事件(比如重新绘制UI以反映您在代码中创建UI对象的更改)。所以在你的代码中,我可能会这样做:
public void StartSession(int port)
{
Form1 form1 = new Form1();
while (true)
{
try
{
// ...
form1.GetData(ClientAdress, update);
// Tell the application to execute other events that are in queue
Application.DoEvents();
// ...
}
catch (Exception ex)
{
form1.Exception(ex);
}
}
}