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

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

问题描述

我有以下设置:

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

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

  1. 客户端发起与服务器的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.

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

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