简体   繁体   中英

Accept all incoming requests on a ServerSocket

I'm temporarily using the following line to create a ServerSocket on my Android phone:

socketl = new ServerSocket(port, 0, InetAddress.getByName("192.168.0.108"));

But it's really annoying and, besides, not user-friendly to change the IP address manually every time the DHCP assigns a new IP to the phone. So I'm searching for a way to get the IP-Address the DHCP-Server gave to my phone.

Ive tries InetAddress.getLocalHost().getHostAddress() , but that only returned the IPv4 loopback ( 127.0.0.1 ).

Is there a way to either get the current local IP(v4) or a way to accept every request, no matter which IP is used to connected? If the IP-Address in the line above isn't the same as the one the client uses, it doesn't work.

You could use

socketl = new ServerSocket(port, 0);

or even

socketl = new ServerSocket(port);

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