To put it simply: after server socket initialization, call accept() method to block and wait. After client socket initialization, call connect method to connect. After successful connection, you can communicate. Textbook:
Socket communication principle: The server initializes the Socket, binds the port, listens to the port, calls Accept to block, and waits for the client to connect. At this time, if a client initializes a Socket and then connects to the server (CONNECT), if the connection is successful, then the connection between the client and the server is established. The client sends a data request, the server receives and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection, ending an interaction.Copy the code
Similar to making A phone call: USER A makes A call to USER B. User B answers the call and OK starts the communication