我想从中的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.LocalEndPoint
和Socket.RemoteEndPoint
属性。
如果您使用的是无连接UDP,则应该使用Begin/EndReceiveFrom()
而不是Begin/EndReceive()
。回调为发送方提供一个EndPoint
。
无论哪种方式,给定一个EndPoint
对象,将其强制转换为IPEndPoint
,并使用其Address
属性访问IP地址。