ServerSocket + client Socket - how do I get IP address of client?

余生颓废 提交于 2019-12-21 16:59:42

问题


I have a ServerSocket instance which is listening for connections. When a client connects to it, I would like to get the IP of the connected socket, but can't seem to find the right method to do so.

public void start() {
    listenSocket = new ServerSocket(port);
    connectionSocket = listenSocket.accept();
}

I've tried calling the following with no luck:

connectionSocket.getLocalAddress();
connectionSocket.getInetAddress.getHostAddress();
listenSocket.getLocalSocketAddress();

None of the above return the correct IP. They either return "/0:0:0:0:0:0:0:1%0" or "0.0.0.0".

What am I doing wrong?


回答1:


connectionSocket.getRemoteSocketAddress();


来源:https://stackoverflow.com/questions/15421592/serversocket-client-socket-how-do-i-get-ip-address-of-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!