udp数据包未通过 [英] udp packet not coming through

查看:531
本文介绍了udp数据包未通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

专用服务器-> Internet->调制解调器(telenet)->路由器->客户端

Dedicated server --> Internet --> Modem (telenet) --> Router --> client

  1. 客户端启动与服务器的tcp连接以在服务器上注册自己,并提供以下信息:
    • 客户端的mac地址
    • 外部ip;这是通过使用从whatsmyip.org下载的webclient字符串来检索的.
  1. The client initiates a tcp connection with the server to register itself on the server, and gives through following info:
    • mac address of the client
    • external ip; this is retrieved by using webclient string download from whatsmyip.org
  • 为通知客户端,服务器将服务器的udp数据包发送到调制解调器(向外部IP,较早从客户端接收到),同时客户端正在监听udp数据包在路由器后面.
  • To notify the client, the server sends a udp packet from the server to the modem (to the external ip, earlier received from the client), in the meanwhile the client is listening for udp packets behind the router.

问题是我没有收到任何数据包.我的情况是否可行,我该怎么办?

要求:

  • 通过在路由器上启用端口转发来解决此问题
  • 服务器具有固定的IP
  • 有时可以断开客户端与互联网的连接
  • 该解决方案必须在不同类型的路由器上工作
  • 发送数据包的两个端口收到的都一样
  • 所有编程均在C#中完成
  • 服务器在有更新时通知客户端,客户端可能永远不会轮询服务器以获取更新以防止过载(以防多个客户端同时执行此操作)

Greets Daan&预先感谢

Greets Daan & thanks in advance

编辑:
服务器上的代码示例:

UdpClient udpSender = new UdpClient();
IPEndPoint localServerGateway = new IPEndPoint(IPAddress.Parse(externalIp), 8003);
string message = "testmessage";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
try
{
     udpSender.Send(messageBytes, messageBytes.Length, localServerGateway);
}
catch (Exception error)
{
     Console.WriteLine("Error while sending message: " + error.ToString());
}
udpSender.Close();

客户端的代码示例:

private void listenForMasterSyncRequest()
{
        bool done = false;
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 8003);

        try
        {
            while (!done)
            {
                byte[] bytes = masterSyncUdpListener.Receive(ref groupEP);
                handleMessage(bytes, bytes.Length, true); // handles incoming messages, this is never reached because no packets are received :-(
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("An error occured while listening to server broadcast updates: " + e.ToString());
        }
        finally
        {
            masterSyncUdpListener.Close();
        }
    }

推荐答案

由于您无法控制路径中的NAT设备,因此这里唯一有效的方法是使用TCP作为主要传输方式.

Since you don't have control of the NAT devices in the path, the only sane way here is to use TCP as your main transport.

这篇关于udp数据包未通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆