简体   繁体   English

在Java中使用套接字的本地客户端的ID

[英]ID for local client using sockets in java

I want get unique ID for my client connected by a server. 我想获取由服务器连接的客户端的唯一ID。 I have many clients, but, each one want has one different ID. 我有很多客户,但是每个客户都有一个不同的ID。 An close solution is similar to this: 一个接近的解决方案与此类似:

Socket cliente = servidor.accept();

        System.out.println("New connection with" +   
        cliente.getInetAddress().getHostAddress());
        String addr = servidor.getLocalSocketAddress().toString();
        ThreadServidor ts = new ThreadServidor(addr, cliente); 
        ts.start();

The clients have the same code to connect: 客户端具有相同的代码进行连接:

  Socket conexao = new Socket("127.0.0.1", 12345);

I need the information of the own client ip and port connected too. 我也需要自己的客户端IP和连接的端口的信息。 Thanks 谢谢

The following methods can be called on your socket cliente to get the local and remote IP addresses and the local and remote ports. 以下方法可以叫上你的插座cliente获取本地和远程IP地址和本地和远程端口。

getLocalAddress()
getLocalPort()
getInetAddress() // gets the remote address
getPort() // gets the remote port

The combination of all that information is unique. 所有这些信息的组合是唯一的。

More info at the Javadoc: Javadoc上的更多信息:

http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

Just use Socket.getRemoteSocketAddress(). 只需使用Socket.getRemoteSocketAddress(). It embodies the remote host:port, which is all you need at the server end. 它体现了远程host:port,这是服务器端所需的全部。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM