Personal understanding of Socket communication

Because I recently made a two-player network game of Gobang, I made a simple study on the implementation of Socket communication in C#, in order to record my personal understanding and facilitate future learning.

What is Socket

When the application layer communicates data through the transport layer, TCP and UDP encounter the problem of providing concurrent services to multiple application processes at the same time.
Multiple TCP connections or multiple application processes may need to transfer data over the same TCP protocol port. In order to distinguish different application processes and connections, many computer operating systems provide interfaces called sockets
for the interaction between applications and the TCP/IP protocol .

There are three main parameters to distinguish the network communication and connection between different application processes: the destination IP address of the communication, the transport layer protocol (TCP or UDP) used, and the port number used .
The original meaning of Socket is "socket". By combining these three parameters and binding with a "socket" Socket, the application layer can communicate with the transport layer through the socket interface, distinguish the communication from different application processes or network connections, and realize the concurrent service of data transmission.

Socket's TCP communication

  • The server side
    (1) establishes a socket on the server side and starts to monitor connection requests in the entire network.
    (2) When the connection request from the client is detected, the information of the connection request is sent to the client, and the connection with the client is established.
    (3) When the communication is completed, the server closes the Socket connection with the client.
  • The server side
    (1) establishes the Socket of the client, and determines the host name and port of the server to be connected.
    (2) Send a connection request to the server and wait for the server's feedback.
    (3) After the connection is successful, data interaction with the server is performed.
    (4) After the data processing is completed, close its own Socket connection.

Gobang correspondence :

server-side response

1. Response to the connection request
If the client connects to the server successfully, the server sends a response containing the connection success information to the client.
2. Send the response to create the room The
parameter is the Socket of the client, that is, the client sends the information of creating the room to the server, and the server sends the response of creating the room to the client. When the client receives it, the room will be created.
3. Send the response of entering the room
If the server receives the message from the client indicating that it wants to enter the room, it will send the response of entering the room to the creator and joiner of the room respectively.
4. Send a response to exit the room
When the client clicks to exit or closes the window, it will send a message to the server to exit the room. If the sender is the creator of the room, it will send a response to the room's joiner; otherwise, it will send a message to the server to exit the room. The creator of the room sends a response to exit the room.
5. Send the server shutdown message
When the server is shut down, the client sends the server shutdown message
6. The response
parameters of sending the drop message are the room and the drop information in the form of string. When the server receives this information, it packs the information such as the move into a messageBox, and then sends it to the corresponding two clients respectively.
7. Send a request to replay the game The
parameters are the room and the sending client identity. If it is the room creator who sent the restart game request, send a restart game request to another user of the room; otherwise, send another game restart request.
8. Send the judgment result of the re-game The
parameter includes a bool value to indicate the judgment result.

For server-side definitions:

First, two lists are defined to store the players currently connected to the server (including player color, ip, nickname, connected port), and the current room list.
The server assigns an independent and non-repeating ID to each client, and finds the ID according to the authentication parameters when the client logs in. When the client sends a message, it needs to specify an independent identity information of the client, which can be an ID or other. The server can obtain the ID and whether the client is currently connected by querying to respond to or store the message sent by the client. Wait for the client to go online before forwarding.
The two clients create a Scoket with the server respectively, and then the two communicate with the server respectively. The server is equivalent to an intermediary agency, which is independently identified by the ID of the Room and the ID of the player. The server will cache the information sent by the client, and send the information (response) to the corresponding client by using the socket established by the server target client according to the target in the messageBox of the client (ie master/member).

How to achieve alternate chess play without conflict?

Each client, after clicking on the chessboard and successfully generating a chess piece, packs the information into a messageBox and sends it to another client through Socket. After sending, it will disable the response of the mouse click, and then start the clientListener to monitor the response request, until the message from the other party is received again, and the chessboard can be operated again.

How can I achieve the interruption of the connection when one side is down?

When one party closes, it will send a "close" message to the other party and close its own Socket connection. When the other party receives the MessageBox and finds that the command (command) in it is to exit, the "connection disconnected" window will pop up and return to the game lobby (its own Socket connection has not been interrupted).
When the client disconnects from the server, the server closes the Socket connection with the client.

Reference:
TCP communication principle of Socket in C#

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324703775&siteId=291194637