Crocodile
Crocodile

Reputation: 53

Why does SocketAsyncEventArgs.RemoteEndpoint returns null? c#

I am Trying to create a udp server which receives data from many clients. When i try to get remote end point of client after receiving data in server from SocketAsyncEventArgs.RemoteEndpoint property... it retruns null. But all the messages are transfered correctly. I am testing this in my local pc.

Here is my Server sided code:

   static Socket FlashUDP = new Socket(AddressFamily.InterNetworkV6,


SocketType.Dgram, ProtocolType.Udp);
        static IPEndPoint rec_ipep = new IPEndPoint(IPAddress.Parse("fe80:0:0:0:e8dd:
c141:d9ab:80f3%12"), 14086);
        static SocketAsyncEventArgs Sock_Args = new SocketAsyncEventArgs();
        static byte[] dataHolder = new byte[8];

static void Main2(string[] args)
{
    Sock_Args.Completed += Sock_Args_Completed;
    Sock_Args.SetBuffer(dataHolder, 0, 4);
    FlashUDP.Bind(rec_ipep as EndPoint);
    Console.WriteLine("Reciving.. v2");
    FlashUDP.ReceiveAsync(Sock_Args);
}

private static void Sock_Args_Completed(object sender, SocketAsyncEventArgs e)
{
    if(e.RemoteEndPoint == null)
    {
        Console.WriteLine("Remote end point is null");
    }
}
/* Output: 
  Reciving.. v2
  Remote end point is null*/

Is this a bug or are there other ways to do the same thing? Thanks!

Upvotes: 2

Views: 843

Answers (1)

Crocodile
Crocodile

Reputation: 53

The Answer was simple,

I dont know what is error using ReciveAsync(); But if we set socketAsyncEventArgs.RemoteEndpoint = new Ipendpoint(Ipaddress.Any, 0); and then call RecieveFromAsync(socketAsyncEventArgs);

now when we print

e.RemoteEndPoint() in Receive callback function it gives the remote endpoint.

Upvotes: 0

Related Questions