UDP packet does not pass

I have the following setup:

Dedicated server β†’ Internet β†’ Modem (telenet) β†’ Router β†’ client

  • The client initiates a tcp connection with the server to register with the server and provides the following information:
    • Client MAC Address
    • external ip; this is retrieved by downloading the webclient line from whatsmyip.org
  • Some updates happen on the server, and, of course, the client must be notified, so the client can independently start a synchronization session:
    • To notify the client, the server sends the udp packet from the server to the modem (to the external ip previously received from the client), while the client listens for udp packets behind the router.

The problem is that I do not receive any packages. Is my script possible, what should I do?

Requirements:

  • Solving this by enabling port forwarding on the router is not an option
  • The server has a fixed ip
  • The client may be disconnected from the Internet from time to time
  • The solution should work on different types of routers.
  • Both ports on which packets are sent and received are the same
  • All programming is done in C #
  • The server notifies the client when there is an update, the client can never interrogate the server for updates to prevent overload (in case several clients do this at the same time)

Welcomes Daan and thanks in advance

EDIT:
Sample code from the 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(); 

Example code from the 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
source share
2 answers

Since you do not control NAT devices on the go, the only reasonable way here is to use TCP as your primary transport.

+1
source

NAT works by setting up sessions between external and internal hosts. But the session must be initiated on the inside, and in your case, on the client side. Thus, it should work, as the client should poll the server by sending a UDP packet to a specific port on the server, asking if synchronization is needed. The server must send a UDP response from the same port back to the same port to which the client sent the original request. If you do so, the packets will go through the server, otherwise they will not. I know this works, because that is how DNS queries work because of NAT.

+3
source

Source: https://habr.com/ru/post/1399165/


All Articles