【问题标题】:Get IP of incoming client - TCP - VB.Net获取传入客户端的 IP - TCP - VB.Net
【发布时间】:2020-12-17 06:50:32
【问题描述】:

我正在尝试获取传入客户端连接请求的 IP:

1_ My computer scan all the IP in my network
2_ When it find a valid IP , it send to it a connection request 
3_ The destination computer will have to get the IP of the client that is sending a connection request and create a 
   New TcpListener(ClientTriyngToConnectIP,64535)

那么有办法获取尝试连接到我的计算机的客户端的 IP 吗?

【问题讨论】:

  • TcpListener 不是这样工作的。

标签: vb.net sockets tcp ip client


【解决方案1】:

最后我找到了一个解决方案:这可以在应用程序中实现,以获取另一台计算机的 IP 尝试通过 TCP 连接到您的计算机

初始化 tcp 监听器:

 Private InputClient As TcpClient
 Public ConnectionRequestListener As TcpListener

 Public sub Listener(IncomingPort as String)

 Dim IPv4_Address As String = ""

 For Each address In Dns.GetHostEntry(Dns.GetHostName()).AddressList
        If address.AddressFamily = AddressFamily.InterNetwork Then
            IPv4_Address &= address.ToString
            Exit For
        End If
 Next

    ConnectionRequestListener = New TcpListener(IPAddress.Parse(IPv4_Address), IncomingPort)
    ConnectionRequestListener.Start()

end sub

接受客户:

InputClient = ConnectionRequestListener.AcceptTcpClient()

获取客户端的IP:

IPAddress.Parse((CType(ConnectionRequestListener.RemoteEndpoint,IPEndPoint)).Address.ToString()).ToString 

【讨论】:

  • 您查询的是TcpListener 正在监听的本地IP,而不是连接到TcpListener 的客户端的远程IP。 AcceptTcpClient()返回一个TcpClient,客户端的IP在TcpClient.Client.RemoteEndPoint中可用
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 2018-09-19
  • 1970-01-01
  • 2015-03-14
  • 2014-05-10
  • 2020-12-08
相关资源
最近更新 更多