简体   繁体   中英

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. Is there any way of finding the IP address of incoming/accepted connections?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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