如何查找连接到服务器的客户端的IP地址?

时间:2022-11-14 09:02:20

My client pc is connected to as server pc via sockets over Ethernet, How do I find the IP of this client from the server side code.
The server is dishing out one socket per client in a new Thread.
When I do a csocket.getLocalAddress().toString() on the client socket I still get the Server IP address. (csocket is the socket that the Server has spawned upon a now client connection and passed it to a new Thread).

我的客户端PC通过以太网上的套接字连接到服务器PC,如何从服务器端代码中找到该客户端的IP。服务器在新线程中为每个客户端抛出一个套接字。当我在客户端套接字上执行csocket.getLocalAddress()。toString()时,我仍然获得服务器IP地址。 (csocket是服务器在现在客户端连接上生成并将其传递给新线程的套接字)。

4 个解决方案

#1


42  

I believe you want to use the remote address instead:

我相信你想要使用远程地址:

csocket.getRemoteSocketAddress().toString();

#2


12  

I think you might be looking for the getInetAddress method of the Socket object.

我想你可能正在寻找Socket对象的getInetAddress方法。

#3


3  

Use getRemoteSocketAddress() instead.

请改用getRemoteSocketAddress()。

#4


1  

Use this code :

使用此代码:

String ip=(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");

#1


42  

I believe you want to use the remote address instead:

我相信你想要使用远程地址:

csocket.getRemoteSocketAddress().toString();

#2


12  

I think you might be looking for the getInetAddress method of the Socket object.

我想你可能正在寻找Socket对象的getInetAddress方法。

#3


3  

Use getRemoteSocketAddress() instead.

请改用getRemoteSocketAddress()。

#4


1  

Use this code :

使用此代码:

String ip=(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");