如何查找連接到服務器的客戶端的IP地址?

[英]How to find the IP Address of Client connected to Server?


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("/","");

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/12/03/bfa37a4c322306ab4a04159bc772b6d2.html



 
  © 2014-2022 ITdaan.com 联系我们: