Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - ServerSocket IP Address

After I opened a server socket, I would like to access its IP address (the one others can use to connect to). When I call

ServerSocket seso = new ServerSocket(port);
System.out.println(seso.getInetAddress());

it displays (0.0.0.0/0.0.0.0), which is only the net mask. What I would like to get is the ip the socket was actually bound to, e.g. 130.75.22.12 (or something similar).

like image 315
MrFreeze Avatar asked Feb 25 '26 23:02

MrFreeze


1 Answers

Freeze

It seems you are searching for water in a desert :).

You can't get the ip address of the ServerSocket which listens to all interfaces in the computer or from any ip address.(Before a device or program connects to the ServerSocket).So for querying all the networking interface's ip address jz follow the link to make a study and use those ip addresses to acheive what you need..

Oracle Java Documentation - Querying Network Interface Addresses.

It seems it would be helpful for everyone.

like image 162
ShihabSoft Avatar answered Feb 28 '26 12:02

ShihabSoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!