无法通过套接字接收 backMessage

时间:2021-04-19 16:17:48

标签: c# winforms sockets

我决定创建一个相当愚蠢的系统来设置正确的端点以将字节数组发送到。在这里,它是如何工作的: 如果您是主持人,您正在等待消息“在这里!”来自正在连接的人。如果您正在连接,则发送“这里!”信息。 主机收到“这里!”后消息,他发送“是的!”消息返回,连接人员正在尝试接收它。 这一切都适用于 ReceiveFrom() 并且它引用 endPoint 以使用它发送其他信息。 主机收到“这里!”消息令人惊讶,但不发送 backMessage 或连接器不接收 backMessage。请帮我解决这个问题。 全部代码如下:

private void button1_Click(object sender, EventArgs e)
        {
            port = int.Parse(textBox2.Text);

            IP = IPAddress.Parse(textBox1.Text);

            Game game = new Game();
            game.Dock = DockStyle.Fill;
            this.Controls.Add(game);
            Controls.Remove(this);
            game.BringToFront();
            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);
                EndPoint endPoint = new IPEndPoint(IP, port);
                sock.SendTo(data, endPoint);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.ToString());
            }
            finally
            {
                Close();
            }

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

            sock.Close();
        }

while (!gameStarted && isHost)
            {
                try
                {
                    localIp = new IPEndPoint(IPAddress.Any, Menu.port);
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    sock.Bind(localIp);

                    Console.WriteLine("Waiting For Players...");
                    StringBuilder builder = new StringBuilder();
                    int bytes = 0;
                    byte[] data = new byte[256 * 10000];

                    endPoint = new IPEndPoint(IPAddress.Any, 0);
                    Console.WriteLine(endPoint);

                    do
                    {
                        bytes = sock.ReceiveFrom(data, ref endPoint);
                        Console.WriteLine(endPoint);
                        builder.Append(Encoding.ASCII.GetString(data, 0, bytes));
                    }
                    while (sock.Available > 0);

                    if (builder.ToString() == "Here!")
                    {
                        Console.WriteLine("Game Started!");
                        try
                        {
                            string backMessage = "Yeah!";

                            byte[] backData = Encoding.ASCII.GetBytes(backMessage);
                            sock.SendTo(backData, endPoint);

                            sock.Close();
                            gameStarted = true;
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine(exc.ToString());
                        }
                        finally
                        {
                            Menu.Close();
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                }
                finally
                {
                    Menu.Close();
                }
            }

while(!isHost && !gameStarted)
            {
                try
                {
                    localIp = new IPEndPoint(IPAddress.Any, Menu.port);
                    sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    sock.Bind(localIp);

                    Console.WriteLine("Waiting For Answer...");
                    StringBuilder builder = new StringBuilder();
                    int bytes = 0;
                    byte[] data = new byte[256 * 10000];

                    endPoint = new IPEndPoint(IPAddress.Any, 0);

                    do
                    {
                        bytes = sock.ReceiveFrom(data, ref endPoint);
                        Console.WriteLine(bytes.ToString());
                        builder.Append(Encoding.ASCII.GetString(data, 0, bytes));
                    }
                    while (sock.Available > 0);

                    if (builder.ToString() == "Yeah!")
                    {
                        Console.WriteLine("Game Started!");
                        sock.Close();
                        gameStarted = true;
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                }
                finally
                {
                    Menu.Close();
                }
            }

对不起,如果我对套接字有误解。希望大家帮帮我

0 个答案:

没有答案