Network Communication Program Arrangement (1)

Source: Internet
Author: User
Document directory
  • Principle of One-or three-way handshake
  • Ii. databases to be introduced
  • 3. Two functions that must be called
  • IV. Implementation of a simple server
  • 5. Implementation of a simple client

 

Principle of One-or three-way handshake

In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection.

First handshake: When a connection is established, the client sends the SYN Packet (SYN = J) to the server and enters the syn_send status. wait for confirmation from the server;

Second handshake: When the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send a SYN Packet (SYN = K), that is, the SYN + ACK packet, the server enters the syn_recv status;

Third handshake: The client receives the server's SYN + ACK package and sends the ACK (ACK = k + 1) Confirmation package to the server. After the package is sent, the client and server enter the established status and complete three handshakes.

As shown in:

Figure 1-1 three-way handshake

After three handshakes, the client and the server start to transmit data. In the above process, there are some important concepts:

Unconnected queue: in the three-way handshake protocol, the server maintains an unconnected queue, which opens an entry for the SYN Packet (SYN = J) of each client, this entry indicates that the server has received the SYN Packet and sent a confirmation to the customer, waiting for the customer's confirmation package. The connection identified by these entries is in the syn_recv state on the server. When the server receives the customer's confirmation packet, it deletes the entry and the server enters the established state.

Backlog parameter: Maximum number of unconnected queues.

SYN-ACK retransmission times the server sends the SYN-ACK package, if the customer does not receive the confirmation package, the server for the first retransmission, wait for a period of time has not received the customer confirmation package, for the second retransmission, if the number of retransmission times exceeds the maximum number of retransmission times specified by the system, the system deletes the connection information from the semi-connection queue. Note that the waiting time for each retransmission is not necessarily the same.

Semi-connection survival time: the maximum time for the semi-connection queue to survive, that is, the maximum time for the service from receiving the SYN packet to confirming that the message is invalid, the maximum waiting time of all retransmission request packets. The semi-join survival time is also called timeout time and syn_recv survival time.

Ii. databases to be introduced

In W32, C ++ programming, if network programming is used, # include <winsock2.h> is required, and then ws2_32.lib is added to configuration Properties> input> additional dependencies, network Communication APIs can be used in actual projects.

3. Two functions that must be called

Wsastartup (): to call any Winsock API function in an application, you must use the wsastartup function to initialize the Winsock service. Therefore, you must call the wsastartup function.

Wsacleanup (): After Windows Sockets is used, the application or dll must call wsacleanup () to unregister it from the implementation of Windows Sockets, and this implementation releases any resources allocated to the application or DLL.

IV. Implementation of a simple server

1. Initialization

2. Create a socket

3. bind to a local device

4. Listen

5. Waiting for connection

6. Conduct communications

7. Close the connection

8. Clear Resources

5. Implementation of a simple client

1. Initialization

2. Create a socket

3. Connection

4. Communication

5. Disable Communication

6. Clear Resources

 

Code: http://files.cnblogs.com/luhouxiang/network_study.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.