Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 从IAsyncResult UDP提取IP_Sockets_Udp_Asyncsocket - Fatal编程技术网

Sockets 从IAsyncResult UDP提取IP

Sockets 从IAsyncResult UDP提取IP,sockets,udp,asyncsocket,Sockets,Udp,Asyncsocket,我想从中的IAsyncResultUDP对象中提取IP地址 EndReceive方法(IAsyncResult ar) 如果可能的话,我怎么做 代码如下: public void End_Receive(IAsyncResult ir) { //Here I need the sender IP ServerSocket.EndReceive(ir); ReceivedMessage = System.Text.UnicodeEncoding.U

我想从中的
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()
。回调为发送方提供了一个
端点


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

提供一些您尝试过的代码。您的标题提到TCP,但您的问题提到UDP。你到底在用什么?对不起,我是说UDP。