由于某种原因无法使用套接字接收信息



首先,我对套接字还很陌生。如果我理解不对劲,我真的很抱歉。

我想在windows.forms中创建一个游戏,使用它你可以检查谁的cps(每秒点击量(更高。你或你是朋友。我创建了两个用户控件,用于游戏和菜单。它看起来是这样的。菜单菜单用户控制

游戏游戏用户控制

我还为切换用户控件做了这件事。

Game game = new Game();
game.Dock = DockStyle.Fill;
this.Controls.Add(game);
Controls.Remove(this);
game.BringToFront();

它工作得非常好。

下一步是为主持人和正在连接的人(从现在开始连接(开始游戏。

我决定,当主机收到消息"时,只创建布尔值设置为true是个好主意;给">

这是代码:

localPort = int.Parse(textBox2.Text);
remotePort = int.Parse(textBox3.Text);
Game.gameStarted = true;
iP = IPAddress.Parse(textBox1.Text);
try
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
string message = "Here!";
byte[] data = Encoding.ASCII.GetBytes(message);
Console.WriteLine(data);
EndPoint endPoint = new IPEndPoint(iP, remotePort);
Console.WriteLine(endPoint);
sock.SendTo(data, endPoint);
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Close();
}
thread = new Thread(Game.Multiplayer);
thread.Start();

为了得到信息:

Menu menu = new Menu();
while (!gameStarted)
{
try
{
IPEndPoint localIp = new IPEndPoint(IPAddress.Loopback, Menu.localPort);
menu.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
menu.sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
menu.sock.Bind(localIp);
Console.WriteLine("Waiting For Players...");
StringBuilder builder = new StringBuilder();
int bytes = 0;
byte[] data = new byte[256];
EndPoint remoteIP = new IPEndPoint(IPAddress.Loopback, 0);
do
{
bytes = menu.sock.ReceiveFrom(data, ref remoteIP);
builder.Append(Encoding.ASCII.GetString(data, 0, bytes));
}
while (menu.sock.Available > 0);
if (builder.ToString() == "Here!")
{
Console.WriteLine("Game Started!");
gameStarted = true;
}
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}

它也非常好用。

问题是它没有接收或发送点击。

这是发送点击的代码(每当你点击按钮1时就会发生(:

private void button1_Click(object sender, EventArgs e)
{
Menu menu = new Menu();
Game game = new Game();
if (gameStarted)
{
yourClicks += 1;
textBox2.Text = yourClicks.ToString();
try
{
menu.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
menu.sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
string message = yourClicks.ToString();
byte[] data = Encoding.ASCII.GetBytes(message);
EndPoint remotePoint = new IPEndPoint(Menu.iP, Menu.remotePort);
menu.sock.SendTo(data, remotePoint);
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}
}

这里是接收点击:

if (gameStarted == true)
{
menu = new Menu();
game = new Game();
while (true)
{
try
{
IPEndPoint localIp = new IPEndPoint(IPAddress.Loopback, Menu.localPort);
menu.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
menu.sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
menu.sock.Bind(localIp);
Console.WriteLine("Counting Clicks!");
StringBuilder builder = new StringBuilder();
int bytes = 0;
byte[] data = new byte[256];
EndPoint remoteIP = new IPEndPoint(IPAddress.Loopback, 0);
do
{
Console.WriteLine("Waiting for Clicks...");
bytes = menu.sock.ReceiveFrom(data, ref remoteIP);
builder.Append(Encoding.ASCII.GetString(data, 0, bytes));
Console.WriteLine(builder.ToString());
}
while (menu.sock.Available > 0);
game.textBox1.Text = builder.ToString();
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
finally
{
Menu.Close();
}
}
}

这一切都发生在每当你点击";主机";或";连接";按钮

thread = new Thread(Game.Multiplayer);
thread.Start();

我希望你们能帮助我。我不知道为什么它不起作用,因为它收到了";给"非常好,但根本不会收到点击。

答案很简单。你只需要在得到信息后关闭你的插座。我刚加了插座。Close((在使用我得到的信息后,一切都很好。

最新更新