如何识别客户端(客户端套接字)? [英] How the clients (client sockets) are identified?

查看:176
本文介绍了如何识别客户端(客户端套接字)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,通过 serverSocket = new ServerSocket(portNumber)我们创建一个潜在可以监听指示端口的对象。通过 clientSocket = serverSocket.accept()我们强制服务器套接字侦听其端口并从任何客户端接受连接尝试连接到服务器通过与服务器相关联的端口。当我说客户端尝试连接到服务器时,我的意思是客户端程序执行nameSocket = new Socket(serverIP,serverPort)。

To my understanding by serverSocket = new ServerSocket(portNumber) we create an object which potentially can "listen" to the indicated port. By clientSocket = serverSocket.accept() we force the server socket to "listen" to its port and to "accept" a connection from any client which tries to connect to the server through the port associated with the server. When I say "client tries to connect to the server" I mean that client program executes "nameSocket = new Socket(serverIP,serverPort)".

连接到服务器,服务器接受此客户端(即创建与此客户端相关联的客户端套接字)。

If client is trying to connect to the server, the server "accepts" this client (i.e. creates a "client socket" associated with this client).

如果新客户端尝试连接到服务器,服务器创建另一个客户端套接字(与新客户端相关联)。但是服务器如何知道它是一个新客户端还是已经存在其套接字的旧客户端?或者,换句话说,如何识别客户端?通过他们的IP?通过他们的IP和端口?由一些签名?

If a new client tries to connect to the server, the server creates another client socket (associated with the new client). But how the server knows if it is a "new" client or an "old" one which has already its socket? Or, in other words, how the clients are identified? By their IP? By their IP and port? By some "signatures"?

如果老客户端尝试再次使用Socket(serverIP,serverIP)会发生什么?服务器是否创建与此客户端关联的第二个套接字?

What happens if an "old" client tries to use Socket(serverIP,serverIP) again? Will server create the second socket associated with this client?

推荐答案

服务器侦听地址和端口。例如,您的服务器的IP地址是10.0.0.1,它正在侦听端口8000.

The server listens on an address and port. For example, your server's IP address is 10.0.0.1, and it is listening on port 8000.

您的客户端IP地址是10.0.0.2,客户端连接到10.0.0.1端口8000的服务器。在TCP连接中,您将提供要连接到的服务器的端口。你的客户端实际上会获得自己的端口号,但是你不控制这个,并且在每个连接上都会有所不同。客户端选择要连接的服务器端口,而不是与之连接的客户端端口。

Your client IP address is 10.0.0.2, and the client "connects" to the server at 10.0.0.1 port 8000. In the TCP connect, you are giving the port of the server that you want to connect to. Your client will actually get its own port number, but you don't control this, and it will be different on each connection. The client chooses the server port that it wants to connect to and not the client port that it is connecting from.

例如,在第一个连接上,您的客户端可能会获得客户端端口12345.它从10.0.0.2端口12345连接到服务器10.0.0.1端口8000.您的服务器可以看到客户端正在连接哪个端口在连接的一侧调用getpeername。

For example, on the first connection, your client may get client-side port 12345. It is connecting from 10.0.0.2 port 12345 to the server 10.0.0.1 port 8000. Your server can see what port the client is connecting from by calling getpeername on its side of the connection.

当客户端第二次连接时,端口号将不同,例如端口12377.服务器可以看到通过在第二个连接上调用getpeername - 它将在客户端看到不同的端口号。 (getpeername还显示客户端的IP地址。)

When the client connects a second time, the port number is going to be different, say port 12377. The server can see this by calling getpeername on the second connection -- it will see a different port number on the client side. (getpeername also shows the client's IP address.)

此外,每次在服务器上调用accept时,都会得到一个新的套接字。你仍然有原始的套接字监听,并在每个接受你得到一个新的套接字。在接受的套接字上调用getpeername以查看连接来自哪个客户端端口。如果两个客户端连接到您的服务器,您现在有三个套接字 - 原始侦听套接字和两个客户端的每个的套接字。

Also, each time you call accept on the server, you are getting a new socket. You still have the original socket listening, and on each accept you get a new socket. Call getpeername on the accepted socket to see which client port the connection is coming from. If two clients connect to your server, you now have three sockets -- the original listening socket, and the sockets of each of the two clients.

您可以有多个客户端同时连接到同一服务器端口8000。并且,许多客户端可以从同一客户端端口(例如端口12345)连接,而不是来自相同的IP地址。从相同的客户端IP地址,例如10.0.0.2,到服务器端口8000的每个客户端连接将来自唯一的客户端端口,例如。 12345,12377等。您可以通过IP地址和端口的组合来区分客户端。

You can have many clients connected to the same server port 8000 at the same time. And, many clients can be connected from the same client port (e.g. port 12345), only not from the same IP address. From the same client IP address, e.g. 10.0.0.2, each client connection to the server port 8000 will be from a unique client port, e.g. 12345, 12377, etc. You can tell the clients apart by their combination of IP address and port.

同一客户端也可以有多个连接到服务器时间,例如一个连接从客户端端口12345和另一个从12377同时。按客户端我的意思是始发IP地址,而不是特定的软件对象。您只会看到两个具有相同客户端IP地址的活动连接。

The same client can also have multiple connections to the server at the same time, e.g. one connection from client port 12345 and another from 12377 at the same time. By client I mean the originating IP address, and not a particular software object. You'll just see two active connections having the same client IP address.

此外,最终随着时间的推移,客户端地址和客户端端口的组合可以重用。也就是说,最后,您可能会看到一个新的客户端从10.0.0.2端口12345,长期在10.0.0.2端口12345已断开连接的第一个客户端。

Also, eventually over time, the combination of client-address and client-port can be reused. That is, eventually, you may see a new client come in from 10.0.0.2 port 12345, long after the first client at 10.0.0.2 port 12345 has disconnected.

这篇关于如何识别客户端(客户端套接字)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆