0

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

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

游戏 游戏用户控制

我也做了这件事来切换用户控件。

 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();
                }
            }

它也完全正常。

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

这是发送点击的代码(每当您点击 button1 时都会发生):

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();

我希望你们帮助我。我不知道为什么它不起作用,因为它收到“这里!” 绝对没问题,但根本没有收到点击。

4

1 回答 1

0

答案很简单。您只需要在获得信息后关闭您的套接字。在使用我得到的信息后,我刚刚添加了 socket.Close() 并且一切正常。

于 2021-04-13T16:57:21.907 回答