简体   繁体   English

返回IP地址/端口的套接字属性

[英]Socket property that returns IP address/Port

I am trying to get a String representation of the IP Address and Port that my Socket has been initialized with. 我正在尝试获取我的Socket已初始化的IP AddressPortString表示形式。 I am using this data to print in a message box for the user if an error occurs. 如果发生错误,我将使用此数据在用户的消息框中打印。

Using the WinSock control in VB6 the following code was used: 在VB6中使用WinSock控件,使用了以下代码:

"Could not open TCP Connection to " & frmMain.winSock1.RemoteHost & ":" 
          & frmMain.winSock1.RemotePort

Any help at all would be appreciated. 任何帮助将不胜感激。

The Socket does not know the remote IP/Port until it actually connects to the server. Socket在实际连接到服务器之前才知道远程IP /端口。 You have to provide a destination to its Connect() method first. 您必须先为其Connect()方法提供目标。 If Connect() fails, you have to know the destination you provided so you can report it, you cannot query the Socket for it. 如果Connect()失败,则必须知道您提供的目的地,以便可以报告它,而无法向Socket查询。 If you connect asynchronously, you will have to remember that destination somewhere so you can retrieve it when needed. 如果异步连接,则必须在某个位置记住该目标,以便在需要时可以检索它。

However, if Connect() succeeds, you can query the RemoteEndPoint property to get the actual IP/Port that the Socket connected to. 但是,如果Connect()成功,则可以查询RemoteEndPoint属性以获取Socket连接到的实际IP /端口。 This is particularly useful when connecting to a hostname, to discover what IP address the hostname resolved to. 当连接到主​​机名,以发现主机名解析为哪个IP地址时,这特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM