本文介绍了ServerSocket + client Socket - 如何获取客户端的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个正在侦听连接的ServerSocket实例。当客户端连接到它时,我想获得连接套接字的IP,但似乎无法找到正确的方法。
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();
以上都不会返回正确的IP。它们要么返回/ 0:0:0:0:0:0:0:1%0或0.0.0.0。
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".
我做错了什么?
推荐答案
connectionSocket.getRemoteSocketAddress();
这篇关于ServerSocket + client Socket - 如何获取客户端的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!