udp packet not coming through

2020-06-28 03:03发布

I have the following setup:

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

  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
  2. Some updates occur on the server and of course the client needs to be notified, so the client can start a sync session on its own:
    • 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.

The problem is that I'm not receiving any packets.. Is my scenario possible, what should I do?

Requirements:

  • Solving this by enabling port-forwarding on the router isn't an option
  • The server has a fixed ip
  • The client can be disconnected from the internet, at times
  • The solution has to work on different kinds of routers
  • Both ports at which packets are send & received are the same
  • All programming is done in C#
  • The server notifies the client when there is an update, the client may never poll the server for updates to prevent overload (in case several clients are doing this the same time)

Greets Daan & thanks in advance

EDIT:
Code example from server:

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();

Code example from client:

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();
        }
    }

2条回答
老娘就宠你
2楼-- · 2020-06-28 03:11

NAT works by setting up sessions between external and internal hosts. But the session must be initiated on the internal side, and in your case that's the client side. So the way it has to work is that the client has to poll the server, sending a UDP packet to a particular port on the server asking if a sync is needed. The server must send a UDP response from that same port back to the same port the client sent the original request. If you do it this way packets from the server will get through, otherwise they will not. I know this works because this is exactly how DNS lookups work from behind NAT.

查看更多
Viruses.
3楼-- · 2020-06-28 03:24

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.

查看更多
登录 后发表回答