简体   繁体   English

UDPClient.Receive()停止运行任何代码

[英]UDPClient.Receive() stops any code from running

I'm trying to create a simple game using the XNA Framework. 我正在尝试使用XNA Framework创建一个简单的游戏。 I'm starting to implement multiplayer over LAN. 我开始通过LAN实现多人游戏。 Everything has been going well but now I'm running into a problem with UDPClient.Receive. 一切进展顺利,但现在我遇到了UDPClient.Receive的问题。

When the server is running, it is supposed to have two listeners(using UDPClient.Receive), on different ports. 服务器运行时,应该在不同的端口上有两个侦听器(使用UDPClient.Receive)。 One listener is waiting for any incoming connection requests for people who want to join the game and respond accordingly. 一位侦听器正在等待想要加入游戏并做出相应响应的人员的任何传入连接请求。 The other listens for constant updates on the player's positions so it can keep all players' views synchronised. 另一个侦听玩家位置的不断更新,以使所有玩家的视图保持同步。 The problem is that I don't know how to code in the first listener. 问题是我不知道如何在第一个侦听器中编码。

When the listener for connection request starts, all the code freezes, and it doesn't start until that listener will receive something. 当用于连接请求的侦听器启动时,所有代码都冻结,并且直到该侦听器将接收到某些内容后,它才会启动。 How would I code it so it is just running in the background? 我将如何编码使其仅在后台运行?

Here is my code for the connection listener: 这是我的连接侦听器代码:

public class Connect
{
    public static void WaitForConnections()
    {
        UdpClient udpc2 = new UdpClient(2054);
        IPEndPoint ep = null;

        Random rnd = new Random();

        Console.WriteLine("Waiting for connections...");
        byte[] joinrequest = udpc2.Receive(ref ep);
        if (Encoding.ASCII.GetString(joinrequest) == "join")
        {
            Console.WriteLine("Attempting to join");
            if (Server.ConnectedPlayers.Count < Server.MaxPlayers)
            {
                byte[] newShooter = DataControls.ClassToByteArray(new ShooterObject(Server.ConnectedPlayers.Count + 1, new Vector2(((Server.ConnectedPlayers.Count + 1) * 100) + 22, 70), new byte[3] { (byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255) }));
                udpc2.Send(newShooter, newShooter.Length, ep);
                Console.WriteLine("Joined successfully");
            }
            else
            {
                byte[] error = Encoding.ASCII.GetBytes("full");
                udpc2.Send(error, error.Length, ep);
                Console.WriteLine("Too many players");
            }
        }
    }
}

You need to use a background worker thread or equivalent (look at Task and threads generally) but to help you get going in basic full example: 您需要使用后台工作线程或等效线程(通常查看Task和线程),但为了帮助您进入基本的完整示例,请执行以下操作:

using System;
using System.ComponentModel;
using System.Threading;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main()
        {
            // Create our listener and start listening...
            var sammpleListener = new SampleListener();
            sammpleListener.StartListening();

            // Run forever...
            Console.WriteLine("Waiting for players to join...");
            Console.WriteLine("Press Ctrl+C to stop!");
            for (var counter = 1;; counter++)
            {
                Console.WriteLine("Main thread working: {0}...", counter);
                Thread.Sleep(200);
            }
        }

        internal class SampleListener
        {
            private readonly BackgroundWorker _udpWaitForPlayer;

            public SampleListener()
            {
                _udpWaitForPlayer = new BackgroundWorker();
                _udpWaitForPlayer.DoWork += _udpWaitForPlayer_DoWork;
            }

            public void StartListening()
            {
                _udpWaitForPlayer.RunWorkerAsync();
            }

            private void _udpWaitForPlayer_DoWork(object sender, DoWorkEventArgs e)
            {
                const int junkSample = 10;
                for (var i = 1; i <= junkSample; i++)
                {
                    // Notice how this doesn't make the main thread sleep / hang...
                    Console.WriteLine("Background worker working: {0} of {1}...", i, junkSample);
                    Thread.Sleep(1000);
                }
                Console.WriteLine("Background worker: Finished!");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM