Java client server application

I found out that the Server application creates a ServerSocket in a specific port,

ServerSocket ServerSock=new ServerSocket(9000);

and the client makes a socket connection to the server application,

Socket sock=new Socket("127.0.0.1","9000");

Thus, the client knows the IP address and port of the server, I am confused how and when the server receives information about the client. Please, help.

Thanks in advance!

+3
source share
3 answers

The server "listens" for incoming connections from clients. Just imagine the port number as the door number, and the server is waiting for guests at that door.

Therefore, when the server application executes serverSock.accept (), it actually blocks and waits for clients to arrive.

, accept() Socket, .

Socket , . :

ServerSocket serverSock=new ServerSocket(9000);

Socket clientSock = serverSock.accept(); //this will wait for a client

System.out.println("Yay we have a guest! He coming from " + clientSock.getInetAddress());
+6

ServerSock.accept(). - .

+2

, IP .
.
ephimeral , TCP- ( ), IP .
, , .

0

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


All Articles