2010-06-29 52 views
0
ipEndReceive = new IPEndPoint(IPAddress.Parse("127.0.0.1"), receivePort); 
receiveSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream 
          , ProtocolType.Tcp); 

ErrorLog.WritetoErrorlog("Trying to Bind IP Address Stored in Variable : " 
          +ipEndReceive.Address.ToString() 
          +"\n Port No :"+ipEndReceive.Port.ToString(), logPath); 

receiveSock.Bind(ipEndReceive); 


ErrorLog.WritetoErrorlog("\nRemote IP Address : " 
        + ((IPEndPoint)receiveSock.RemoteEndPoint).Address.ToString() 
        + "\n Local IP:" 
        + ((IPEndPoint)receiveSock.LocalEndPoint).Address.ToString() 
        , logPath); 

这里由于这我不是能够得到请求已收到其中远程IP地址receiveSock.RemoteEndPoint返回我的EndPoint,而不是IPEndPoint的情况下获得一个IP地址。 我们有什么办法从这个套接字中获取它。如何从一个IPEndPoint在插座

+0

可能重复[如何从Socket获取IP地址](http://stackoverflow.com/questions/3138885/how-to-get-an-ip-address-from-socket) – 2010-06-29 08:24:43

+0

@ ho1:我认为这与前一个问题密切相关我问。但这里的问题有点不同,在这里我指出从** EndPoint获取IP地址** – 2010-06-29 08:27:00

+1

EndPoint是一个抽象类,我没有得到问题,演员是否失败? – 2010-06-29 08:27:43

回答

0

套接字连接后将设置套接字RemoteEndpoint。

这意味着绑定后记录RemoteEndpoint不正确。

RemoteEndPoint在调用Accept或Connect后设置。如果您尝试更早地访问此属性,RemoteEndPoint将引发一个SocketException。如果您收到一个SocketException,请使用SocketException :: ErrorCode属性来获取特定的错误代码。获得此代码后,请参阅MSDN Library中的Windows Sockets第2版API错误代码文档的错误的详细说明。”

(从MSDN)的