How are clients (client sockets) identified?

In my understanding, serverSocket = new ServerSocket(portNumber) we create an object that can potentially "listen" on the specified port. Under clientSocket = serverSocket.accept() we make the server socket โ€œlistenโ€ to its port and โ€œacceptโ€ the connection from any client that tries to connect to the server through the port connected to the server. When I say "the client is trying to connect to the server", I mean that the client program executes "nameSocket = new Socket (serverIP, serverPort)".

If a client tries to connect to the server, the server "accepts" this client (ie, creates a "client socket" associated with this client).

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

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

+4
source share
5 answers

The server listens for the address and port. For example, your server IP address is 10.0.0.1, and it listens on port 8000.

The IP address of the client is 10.0.0.2, and the client "connects" to the server with port 10.0.0.1 8000. In the TCP connection, you specify the port of the server to which you want to connect. Your client will indeed receive its own port number, but you do not control it, and it will be different for each connection. The client selects the server port to which it wants to connect, and not the client port from which it connects.

For example, the first time you connect, your client can get client port 12345. It connects from 10.0.0.2 port 12345 to the server 10.0.0.1 port 8000. Your server can see which port the client connects to by calling getpeername on its connection side.

When the client connects a second time, the port number will 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 IP address of the client.)

In addition, every time you call accept on the server, you get a new socket. You still have the original socket listening, and on each of them you get a new socket. Call getpeername on the received socket to find out which client port the connection is from. If two clients connect to your server, now you have three sockets - the original socket for listening and sockets for each of the two clients.

You can simultaneously connect 8000 clients to one server port at the same time. And many clients can be connected from the same client port (for example, port 12345), but not from the same IP address. From the same client IP address, for example. 10.0.0.2, each client connection to server port 8000 will be made from a unique client port, for example. 12345, 12377, etc. You can tell customers separately by their combination of IP address and port.

The same client can have several connections to the server at the same time, for example. one connection to client port 12345 and another to 12377 at the same time. By client, I mean the source IP address, not a specific software object. You will see two active connections having the same client IP address.

In addition, over time, the combination of client address and client port can be reused. That is, in the end, you can see that the new client comes in with 10.0.0.2 port 12345, long after the first client in 10.0.0.2 port 12345 is disabled.

+15
source

Each TCP connection has a four as an identifier (src port, src address, dest port, dest address).

Whenever your server accepts a new client, a new Socket is created and it does not depend on all the sockets created so far. Customer identification is not considered in any way.

You do not need to think about sockets connected with "clients", they are connected with ip and port, but there is no direct correlation between them.

If the same client tries to open another socket by creating a new one, you will have two unconnected sockets (because the ports will differ exactly). This is because the client cannot use the same port to open a new connection, so the four will be different, the same client ip, the same server ip, the same server port, but a different client port.

EDIT your questions:

  • clients do not specify a port, because it is accidentally selected from free (> 1024, if I'm not mistaken) from the underlying operating system
  • it is impossible to open a connection with the client using the same port, the operating system will not allow you to do this (in fact, you will not specify any port at all), and in any case, it will tell you that the port is already connected to the socket, so this problem can't happen.
  • whenever a server receives a new connection request, it is considered new, because also if the ip address of the same port differs exactly (in case of resending the old packet or similar reservations, I think the request will be rejected)

By the way, all of these situations are clearly explained in TCP RFC here .

+2
source

By definition, this is not a Java-related issue, but the network as a whole, since Sockets and SeverSockets apply to any programming language that supports network compatibility.

A socket is limited to a local port. The client will open a connection to the server (operating system / drivers / adapters / hardware / line /.../ line / hardware / adapters / drivers / server OS). This โ€œconnectionโ€ is made over a protocol called IP (Internet Protocol) when you are connected to the Internet. When you use Sockets, it will use a different protocol, which is TCP / IP.

The Internet Protocol identifies nodes on the network with two things: their IP address and their port. The TCP / IP protocol will send messages using the IP address and receive messages correctly.

Now; answer your question: it all depends! It depends on your drivers, your adapters, your equipment, your line. When you connect to your local machine, you will not be able to get further than the adapter. Hardware is not necessary as no data is transmitted over the line. (Although you often need equipment before you can get the adapter.)

By definition, an Internet protocol defines a connection as a pair of nodes (thus, four things: two IP addresses and two ports). In addition, the Internet protocol determines that one node can only use one port at a time to initiate a connection with another node (note: this only applies to the client, not the server).

To answer the second question: if there are two sockets: "new" and "old". Since the connection through the Internet protocol is a pair of nodes, and the nodes can use only one port per connection, the ports of the "new" and "old" should be different. And since this is different, the "new" client can be distinguished from the "old" one, since the port number is different.

0
source

I think this raises the question of why you don't care whether the client is new or old. What's new and old? For example, a web browser may connect to a web server to request a web page. This will create a connection, so serverSocket.accept() will return a new Socket . Then the connection is closed by the web browser.

Afer after a couple of minutes, at the end, click on the link on the web page, and the browser requests a new page on the server. This will create a connection, so serverSocket.accept() will return a new Socket .

Now the web server does not care if it is a new or an old client. He just needs to send the requested page. If the server do care if the "client" has already requested a page in the past, he has to do this by using some of the information in the protocol used in the socket. Check out http://en.wikipedia.org/wiki/OSI_model In this case, the ServerSocket and Socket values ServerSocket at the transport level. The question "does this client request a page on the server" should answer information about the session or even about the application level.

In the example of a web browser / server, the http protocol (which is the application) contains information about who this browser is in the request parameters (the browser transmits cookie information with each request). The HTTP server can then set / read cookie information to find out if the browser is connected before and, ultimately, maintain a server-side session for that browser.

So, back to your question: why does it bother you if this is a new or old client?

0
source

A socket is identified by:

(local IP address, local port, remote IP address, Remote port, IP protocol (UDP / TCP / SCTP / etc.)

And what is the information used by the OS to map packets / data to the descriptor descriptor / descriptor of your program. For some types of sockets (for example, a non-connected UDP socket), the remote port / remote IP address can be wildcards.

0
source

Source: https://habr.com/ru/post/1304026/


All Articles