1

I have a UDP Server and client which works on localhost when the server and client are both behind the same router.

When connecting from external IP - the IPEndPoint holds the server IP.

Server receiver:

private void Recv(IAsyncResult res)
{
    IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);

    try
    {
        byte[] data = udpClient.EndReceive(res, ref clientEndPoint);
        udpClient.BeginReceive(new AsyncCallback(Recv), null);

        //Process codes
        RaiseDataReceived(new ReceivedDataArgs(clientEndPoint.Address, clientEndPoint.Port, data));
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        throw;
    }
}

When the server receives a message from external client, the clientEndPoint.Address holds the server's address, not the client address. Has anyone experienced this? I've tried multiple ways of receiving messages from clients without luck.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
MatMat
  • 878
  • 3
  • 8
  • 24
  • That's by design if you read Microsoft documentation on `Receive` and `ReceiveFrom`. Only the latter capture packet information, which includes the source IP address. – Lex Li Aug 29 '18 at 02:20
  • How is that a duplicate. I'm using async which isn't addressed in that question? – MatMat Aug 29 '18 at 08:00
  • `BeginReceive` and `BeginReceiveFrom` mirror the same difference. More importantly, today you should use `ReceiveAsync` or `ReceiveFromAsync` due to the paradigm shift of async/await. Spend some time learning the history of async support of .NET, and you will understand the patterns. – Lex Li Aug 29 '18 at 12:16
  • @LexLi I'm currently at a point where I'm about to go nuts. Nothing I do changes the fact that when I get the clientEndPoint it's my public IP I get and not the client's public IP. Would you mind helping me out? I've tried BeginReceiveMessageFrom aswell as ReceiveAsync – MatMat Aug 30 '18 at 18:39
  • I won't be able to help, as my experience on such (#SNMP Library) did not recall any similar cases. If you want more help from SO, provide a complete sample so that anyone can reproduce on their machines. – Lex Li Aug 30 '18 at 18:56

0 Answers0