Network Programming programmers view network protocols

Source: Internet
Author: User

Network ProtocolThe format of data transmitted over the network. ForNetwork ProgrammingFor beginners, it is not necessary to have a deep understanding of TCP/IP protocol clusters. Therefore, it is not very appropriate for beginners to read the TCP/IP protocol, this is because an in-depth understanding of the TCP/IP protocol is an improvement stage in network programming, and it is also necessary to go deep into the underlying layer of network programming.

For general network programming, more is concerned with the logical data content transmitted on the network, that is, more is the network protocol on the application layer, therefore, the subsequent content will introduce the concept of network protocol based on actual application data.

So what is network protocol? Let's look at a simple example below. At the Spring Festival Gala, "Small Shenyang" and Zhao Benshan cooperated in the sketch "no money", Xiao Shenyang and Zhao Benshan designed an agreement between them. The content of the agreement is:

If the order is expensive, no.

According to the provisions of this Agreement, the following dialog is available:

Zhao Benshan: 4-pound lobster

Small Shenyang: (After judgment, the price is relatively high), no

Zhao Benshan: abalone

Small Shenyang: (After judgment, the price is relatively high), no

This is an agreement between the two parties. In fact, the essence of this agreement is the same as that of the network protocol. The essence of the network protocol is also a convention between client programs and server programs for data. It is only because it is based on computers, so more is to use numbers to represent content, this is more abstract.

Next we will give a simple example to introduce some basic network protocol design knowledge. For example, you need to design a simple network program: a network calculator. That is, enter the numbers and operators to be calculated on the client side, implement the calculation on the server side, and feedback the calculation results to the client. In this example, two data formats need to be agreed: the data format sent from the client to the server, and the data format fed back from the server to the client.

You may think this is relatively simple. For example, the numbers entered by the client are 12 and 432 in sequence, and the input operator is the plus sign. The most likely data format is to form the string "12 + 432 ", this format is indeed easy to read, but the logic is troublesome when the server side is computing, because the string needs to be split before calculation, therefore, there are several available data formats:

"12,432, +" format: the first digit, the second digit, and the operator

"12, +, 432" is in the format of the first digit, operator, and second digit.

In fact, the above two data formats are very similar and easy to read. After the server receives the data format, use "," as the separator to separate the strings.

Assume that the operator is re-Agreed. For example, the agreed number 0 represents +, 1 represents subtraction, 2 represents multiplication, and 3 represents division. The overall format follows the first format, the protocol data produced in the preceding figure is:

"12,432, 0"

This is a basic sending protocol.

Another protocol format that needs to be designed is the data format fed back by the server. In fact, the server mainly feedbacks the calculation results. However, when actually receiving data, there may be format errors, in this way, you need to design the data format provided by the server. For example, if the format of the sent data is correct, the result is fed back; otherwise, the string "error" is fed back ". The following data format is available:

Client: "1,111, 1" server: "-110"

Client: "146, 0" server"

Client: "1, 2, 5" server: "error"

In this way, we have designed a most basic network protocol format. From this example, we can see that the network protocol is a format convention, various data formats can be agreed according to the logic requirements. Generally, the design follows the principle of "simple, General, and easy to parse.

For complex network programs, the types of data to be transmitted and the amount of data are relatively large. In this way, you only need to design the data formats in each case in sequence, such as the QQ program, there are many types of network data to be transmitted in this program, so you can follow the Login Format, registration format, message sending format, and so on in the design. Therefore, for complex network programs, more command formats are added, and the workload increases in actual design.

In network programming, for the same network program, two network protocol formats are generally involved: the client sends data format and the server feedback data format. In actual design, one-to-one correspondence is required. This is the basic knowledge of network protocols.

After the network protocol design is complete, during network programming, the corresponding encoding needs to be performed in the program according to the designed protocol format, the code for protocol processing between client programs and server programs is as follows.

The client must complete the following operations:

1. Generation of the client sending protocol format

2. parsing of the server feedback data format

The processing required by the server program is as follows:

1. Server feedback protocol format generation

2. parsing of the client sending protocol format

The generated data here refers to converting the computed data to the specified data format. The parsing here refers to splitting the required data from the feedback data format. When writing the corresponding code, strictly abide by the agreement.

Therefore, for programmers, when writing network programs, they need to first design the network protocol format according to the logic needs, and then follow the protocol format conventions to compile the Protocol generation and resolution code, finally, network programming technology is used to implement the entire network programming function.

Because different network programs use different protocol formats, clients of different network programs cannot be used in common.

For common Protocol formats, such as HTTP (Hyper Text Transfer Protocol, Hypertext Transfer Protocol), FTP (File Transfer Protocol, File Transfer Protocol), SMTP (Simple Mail Transfer Protocol, for more information, see the RFC documentation.

The network protocol format is the core technical secret of a network program, because once the Protocol format leaks, anyone can write the client according to the format, this will affect the implementation of the server, and may also have some other effects.

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.