udp 数据包不通过数据包、不通过、udp

2023-09-09 21:07:41 作者:怎如初

我有以下设置:

专用服务器 --> 互联网 --> 调制解调器(远程网络) --> 路由器 --> 客户端

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

客户端发起与服务器的tcp连接以在服务器上注册自己,并通过以下信息给出:客户端的mac地址外部IP;这是通过使用从 whatsmyip.org 下载的 webclient 字符串来检索的 为了通知客户端,服务器从服务器向调制解调器发送一个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# 完成服务器会在有更新时通知客户端,客户端可能永远不会轮询服务器以获取更新以防止过载(以防多个客户端同时执行此操作)

迎接 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.