简体   繁体   English

Java TCP/IP:在侦听套接字时查找传入连接的 IP 地址

[英]Java TCP/IP: Finding the IP address of an incoming connection while listening on a socket

I have a Java server implementation which listens for incoming TCP/IP connections on a specified port.我有一个 Java 服务器实现,它侦听指定端口上的传入 TCP/IP 连接。 Is there any way of finding the IP address of incoming/accepted connections?有没有办法找到传入/接受连接的 IP 地址?

Yes, upon accepting the incoming connection you would get a Socket instance, so you can get the address to which it is connected as follows:是的,在接受传入连接后,您将获得一个 Socket 实例,因此您可以获得它所连接的地址,如下所示:

String hostAddress = socket.getInetAddress().getHostAddress();

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

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