简体   繁体   English

如何在服务器套接字端检索客户端的IP和PORT?

[英]How to retrieve client's IP and PORT on the server socket end?

I have a socket listener java based running on the desktop end and I have a mobile client, which is able to connect with this socket server without any issues. 我有一个基于java的套接字侦听器在桌面端运行,并且我有一个移动客户端,它可以与此套接字服务器连接而不会出现任何问题。 I want to have the socket connection established for each client separately, when client is trying to access new connection. 当客户端尝试访问新连接时,我想分别为每个客户端建立套接字连接。 I read from this link, Reference link for multiple connection detection 我从此链接中读取了用于多个连接检测的参考链接

about how socket can differentiate for multiple connections. 关于套接字如何区分多个连接的信息。 It is mentioned that based on the Client connection's IP and PORT detected on the socket server and the connection is made unique for each connection. 提到基于套接字服务器上检测到的客户端连接的IP和PORT,并且该连接对于每个连接都是唯一的。 Client PORT will differ for each connection. 每个连接的客户端端口将有所不同。 I read that, we can call getpeername to get the client connection's IP and PORT on the socket server end. 我读到了,我们可以调用getpeername来获取套接字服务器端上客户端连接的IP和PORT。 But, i couldn't see any such API available in Java and then found the below one. 但是,我看不到Java提供的任何此类API,然后找到了下面的API。

ServerSocket server = new ServerSocket(8080);
System.out.println("server.getInetAddress();: " + server.getInetAddress());

But, It is returning always server.getInetAddress():- 0.0.0.0/0.0.0.0 when the mobile client (Simulator) is calling this socket. 但是,当移动客户端(模拟器)调用此套接字时,它总是返回server.getInetAddress():- 0.0.0.0/0.0.0.0

Could someone advise me, why I'm not getting the client's IP and PORT using the above InetAddress API on the socket end? 有人可以告诉我,为什么我没有在套接字端使用上面的InetAddress API获得客户端的IP和PORT吗? Or Do I need to use different approach? 还是我需要使用其他方法?

When the server accept sa connection you get a Socket that represents that connection. 当服务器accept一个连接时,您将获得一个代表该连接的Socket Socket has a getInetAddress() method that will return the remote's address, and a corresponding getPort() method that will return the remote's port. Socket具有将返回远程地址的getInetAddress()方法和将返回远程端口的相应getPort()方法。

For example: 例如:

Socket newSock = s.accept();
InetAddress addr = s.getInetAddress();
int         port = s.getPort();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 IP 地址和端口号连接到服务器套接字? (客户端运行在与服务器不同的机器上) - How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server) 如何在与套接字服务器相同的计算机(IP)上运行套接字客户端? - How to run a socket client on the same computer (IP) as a socket server? 如何在Apache Ignite中查找正在运行的服务器或客户端ip及其端口 - How to find running server or client ip and its port in Apache Ignite 服务器如何获取客户端的IP和端口号TCP? - How do servers get client's IP and port number TCP? 客户端可以通过IP地址和套接字端口使用不同的ISP(互联网服务提供商)连接到服务器 - It is possible for Client connect to server using different ISP(internet service provider) by ip address and socket port 如何在特定端口上搜索响应式套接字服务器(如何在给定端口但没有其ip的情况下连接到服务器)? - How to search for responsive socket servers on a particular port (How to connect to a server given the port but not having its ip)? Java客户端/服务器套接字端口分配 - Java Client/Server socket port assignment Java套接字-客户端和服务器IP地址 - Java socket - client and server IP address 如何从Android Tcp客户端套接字连接到具有公共IP的Java TCP服务器套接字? - How to connect to a Java TCP server socket having Public IP From Android Tcp Client Socket? 服务器如何知道客户端的端口号,它将使用它使用 Java 编程套接字将响应发送到客户端? - How the server knows the port number of the client in which it will use it to send the responses to the client using java programming socket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM