从使用 UdpClient 接收的 UDP 包中获取客户端 IP

发布于 2024-11-06 07:03:10 字数 928 浏览 0 评论 0原文

我正在 系统的帮助下开发一款动作多人游戏.Net.Sockets.UdpClient 类。

它适合两个玩家,因此一个人应该打开服务器并等待传入​​连接。另一个玩家输入主机 IP 并尝试发送“ping”,以确保可以建立连接并且有一个开放的服务器。然后主人用“乒”的一声回应。

一旦游戏运行,双方都必须互相发送udp消息,因此他们都需要对手的ip地址。

当然服务器也可以输入客户端IP,但这对我来说似乎没有必要。

当收到“ping”消息时,如何从 udp 包中获取客户端 IP?

这是我的接收代码(服务器等待 ping):

    private void ListenForPing()
    {
        while (!closeEverything)
        {
             var deserializer = new ASCIIEncoding();
             IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
             byte[] recData = udp.Receive(ref anyIP);
             string ping = deserializer.GetString(recData);
             if (ping == "ping")
             {
                 Console.WriteLine("Ping received.");
                 InvokePingReceiveEvent();
             }
        }
    }

I am developing an action multiplayer game with the help of the System.Net.Sockets.UdpClient class.

It's for two players, so one should open a server and wait for incoming connections. The other player inputs the host IP and tries to send a "ping", to make sure a connection is possible and there is an open server. The host then responds with a "pong".

Once the game is running, both have to send udp messages to each other, so they both need the opponents ip address.

Of course the server could also input the clients IP, but that seems unneccessary to me.

How can I get the clients IP from the udp package when the "ping" message is received?

Here is my receiving code (server waiting for ping):

    private void ListenForPing()
    {
        while (!closeEverything)
        {
             var deserializer = new ASCIIEncoding();
             IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
             byte[] recData = udp.Receive(ref anyIP);
             string ping = deserializer.GetString(recData);
             if (ping == "ping")
             {
                 Console.WriteLine("Ping received.");
                 InvokePingReceiveEvent();
             }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独闯女儿国 2024-11-13 07:03:10

在您的示例中,当客户端连接时,anyIP IPEndPoint 对象将包含客户端连接的地址和端口。

In your example, when a client connects, the anyIP IPEndPoint object will contain the address and port of the client connection.

匿名的好友 2024-11-13 07:03:10
private void ListenForPing()
{
    while (!closeEverything)
    {

         IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
         byte[] recData = udp.Receive(ref anyIP);
         string ping = Encoding.ASCII.GetString(recData);
         if (ping == "ping")
         {
             Console.WriteLine("Ping received.");
             Console.WriteLine("Ping was sent from " + anyIP.Address.ToString() +
                             " on their port number " + anyIP.Port.ToString());
             InvokePingReceiveEvent();
         }
    }
}

http://msdn.microsoft.com/en -us/library/system.net.sockets.udpclient.receive.aspx

private void ListenForPing()
{
    while (!closeEverything)
    {

         IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
         byte[] recData = udp.Receive(ref anyIP);
         string ping = Encoding.ASCII.GetString(recData);
         if (ping == "ping")
         {
             Console.WriteLine("Ping received.");
             Console.WriteLine("Ping was sent from " + anyIP.Address.ToString() +
                             " on their port number " + anyIP.Port.ToString());
             InvokePingReceiveEvent();
         }
    }
}

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.receive.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文