network communication Mode
There are two kinds of existing network communication methods:
1.TCP (Transmission Control Protocol) mode
TCP is a connection-oriented reliability transport protocol, TCP is like a telephone, use this way to communicate, need to establish a dedicated virtual connection, and then reliable data transmission, if the data sent failed, the client will automatically resend the data.
2.UDP (User Datagram Protocol) mode
UDP is a connectionless protocol, each datagram is a separate information, including the full source address or destination address, it is on the network with any possible path to the destination, so can reach the destination, the time to reach the destination and the correctness of the content is not guaranteed. UDP is like sending text messages, this way does not need to establish a dedicated virtual connection, transmission is not reliable, if the delivery failed the client can not obtain.
Comparison Summary:
(1) These two kinds of network communication methods are the actual network programming use, the important data generally uses the TCP method to carry on the data transmission, but the massive non-core data all passes through the UDP way.
(2) Because TCP needs to establish a dedicated virtual connection and verify that the transfer is correct, the TCP method is slightly slower and the amount of data produced is slightly larger than UDP.
(3) When using UDP to transmit data, there is a size limit, each transmitted datagram must be limited to 64KB. TCP does not have this limitation, once the connection is established, the two sides can transfer a large amount of data in a uniform format. (4) UDP is an unreliable protocol, the sender sent the datagram does not necessarily in the same order to reach the receiver. While TCP is a reliable protocol, it ensures that the receiver is fully and correctly getting all the data sent by the sender.
steps in Network programming
1. Client-side programming
The client is the first party to send a communication request, regardless of the client is simple or complex, follow the client programming three steps, as follows:
(1) Establish a connection
The client needs to send the request to the server by Ip:port, if the connection is established, then the connection is a virtual connection, and then the data can be communicated with the server side.
(2) Exchange of data
After the connection is established, the client and server are strictly in accordance with the request-response mode for data communication, the client sends a request, the server side receives the processing response back, if the client does not request, then the server side will not respond.
(3) Close the connection
After the client requests that the server-side response is complete, the previously established connection is closed to release the bound ports and resources such as memory.
2. Server-side
The server side is the one side that is passively waiting for the connection to receive the client's request and process the response. The server side follows the following four steps, as follows:
(4) Listening port
When the server side is turned on, the server side listens to the corresponding port in advance to wait for the client to connect to it.
(5) Get the connection
Once the client and server side of the IP and Port match, at this point, the server will get a connection, the general Network programming server side will use multithreading way to handle the connection of multiple clients, once there is a client connection, will put the client in a separate thread to perform the corresponding data exchange.
(6) Exchange of data
The server side first receives the client's requested data, which is processed after being sent to the client.
(7) Close resources
After the client and server-side data exchange is complete, the connection needs to be disconnected to release the appropriate resources.