2010-06-29 51 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)的