Extract IP from IAsyncResult UDP



我想从中的IAsyncResult UDP对象提取IP地址EndReceive方法(IAsyncResult ar)

如果可能的话,我该怎么做?

这里的代码:

public void End_Receive(IAsyncResult ir)
{
        //Here I need the sender IP
        ServerSocket.EndReceive(ir);
        ReceivedMessage =  System.Text.UnicodeEncoding.Unicode.GetString(buffer);
}

如果使用TCP或连接的UDP,请使用Socket.LocalEndPointSocket.RemoteEndPoint属性。

如果您使用的是无连接UDP,则应该使用Begin/EndReceiveFrom()而不是Begin/EndReceive()。回调为发送方提供一个EndPoint

无论哪种方式,给定一个EndPoint对象,将其强制转换为IPEndPoint,并使用其Address属性访问IP地址。

最新更新