문제

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top