Linux Network Programming----Network Protocol __ Block chain

Source: Internet
Author: User
Tags ack file transfer protocol

What is Linux network programming

Network programming is the writing of such programs that communicate with other programs through a computer network. One of the biggest differences between web programs and ordinary programs is that the Web program is made up of two parts---client and server side. The network program is the first server program to start, waiting for the client's program to run and establish a connection. In general, the service-side program listens on a port until a client's program sends a request. The network communication needs the network communication protocol to hold. Linux systems are programmed by providing sockets for network programming. The network program returns a file descriptor for communication by invoking the socket and several other functions, which we can use as a descriptor for the normal file. This is the benefit of the device-independent nature of Linux. We can communicate data between networks by reading and writing to descriptors.

What TCP/IP protocol

TCP/IP is a communication protocol for communicating with computers connected to the Internet. The TCP/IP protocol uses a hierarchy of 4 tiers, each of which calls its next level of network to fulfill its needs. These 4 layers are:

  Application layer: a layer of communication between applications, such as simple e-mail transfer (SMTP), File Transfer Protocol (FTP), Network Remote Access Protocol (Telnet), and so on.

  Transport Layer: in this layer, it provides data transfer between nodes, communication services between applications, the main function is data format, data recognition and loss of retransmission lights. such as Transmission Control Protocol (TCP), User Datagram Protocol (UDP), and so on, TCP and UDP add data to the packet and transfer it to the next level, which is responsible for transferring data and determining that the data has been delivered and received.

  Interconnection Network layer: responsible for providing the basic data packet delivery function, so that each packet can reach the destination host (but not check whether it is properly received), such as Internet Protocol (IP).

  Network Interface Layer (host-network layer): receives the IP datagram and carries on the transmission, receives the physical frame from the network, the extraction IP datagram transmits to the next layer, to the actual network media management, defines how uses the actual network (like Ethernet, serial line and so on) transmits the data.

Contains a series of protocols for processing data in TCP/IP:

TCP (Transmission Control Protocol)-Communication between applications UDP (User Packet Protocol)-Simple communication between applications IP (Internet Protocol)-communication ICMP between computers (Internet-based Message Control Protocol)-for error and state DHCP (Dynamic Host Configuration Protocol)-for dynamic Addressing

The protocol adopts a layered structure, therefore, the data packet also adopts the layered encapsulation method. The following is an example of the most widely used Ethernet to illustrate the layered encapsulation of its data packets, as follows:

┏━━━━━━━━━━┓

┃TCP Header │tcp Data area ┃TCP

┗━━━━━━━━━━┛

┏━━━━━━━━━━━━━━━┓

┃ip header ┃ ..... IP data area ....... ┃IP

┗━━━━━━━━━━━━━━━┛┏━━━━━━━━━━━━━━━━━━━┓

┃ frame Head ┃ ......... Frame data area .............. ┃ETH

┗━━━━━━━━━━━━━━━━━━━┛

Any communication protocol has a unique message format, TCP/IP protocol is no exception. For communication protocol programming, we must first understand its message format. Because of the TCP/IP protocol using layered model, each layer has a dedicated header, the following is a simple introduction of TCP/IP under the Ethernet Shong text format.

What is a TCP protocol

TCP is a Transport layer protocol for TCP/IP. TCP provides a reliable connection-oriented data flow transport service to the application layer. TCP packs user data into a message segment, it sends data to start a timer, the other end confirms the data received, reorder the data in disorder, discards duplicate data, TCP provides End-to-end flow control, and calculates and validates a mandatory end-to-end test and. TCP is initialized with a three-time handshake.

The TCP message format is as follows:

0 7 8 15 16 23 24 31

┣┳┳┳┳┳┳┳╋┳┳┳┳┳┳┳╋┳┳┳┳┳┳╋┳┳┳┳┳┳┳┫

┃ Source port ┃ Destination Port ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

Sequence Number

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫┃ Confirmation Number

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

┃ head length ┃u A P R S f│ window size ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

┃ checksum ┃ emergency pointer ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

┃ option (0 or more 32bit) ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

┃ Data ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

1. Source port, Destination port: 16bit, identify the remote and local port number.

2. Sequence Number: 32bit, identifies the packet order sent.

3. Confirm Number: 32bit, the serial number of the next packet that you want to receive.

4.TCP head Length: 4bit, indicating how many 32bit the TCP header contains.

5.6bit retention is not available.

6.URG: An emergency pointer indicating that the sender sends the data in an emergency manner to the other end. (6-11 is only 1bit)

7.ack:ack Position 1 indicates that the confirmation number is valid, and if the ACK is 0, then the packet does not contain confirmation information and the confirmation field is omitted.

8.PSH: Represents data with a push flag. Therefore, the request packet can be sent to the receiving application without waiting for the buffer to fill.

9.RST: Used to reset error connections that occur due to host crashes or other reasons, and can also be used to deny illegal packets or deny requests.

10.SYN: Used to establish a connection.

11.FIN: For releasing the connection.

12. Window Size: 16bit, the large small segment of the window indicates how many bytes can be sent after the byte has been confirmed.

13. Checksum: 16bit is designed to ensure high reliability.

14. Optional: 0 or more 32bit, including the maximum TCP load, window ratio, select the postback packet options.


What is a UDP protocol

UDP is a simple data-oriented transport layer protocol, which is a connectionless and unreliable transport protocol, and does not require a 3-handshake to establish a connection. UDP has the disadvantage of not providing packet grouping, assembling, and not sorting packets, that is, when a message is sent, it is not possible to know whether it has arrived safely or completely.

The UDP message format is as follows:

0 7 8 15 16 23 24 31

┣┳┳┳┳┳┳┳╋┳┳┳┳┳┳┳╋┳┳┳┳┳┳╋┳┳┳┳┳┳┳┫

┃ Source port ┃ Destination Port ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

┃UDP length ┃udp Calibration and ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫┃┃

┃ Data ┃

┣┼┼┼┼┼┼┼╋┼┼┼┼┼┼┼╋┼┼┼┼┼┼╋┼┼┼┼┼┼┼┫

What is IP protocol

IP protocol is located at the network layer, it provides unreliable, connectionless datagram transmission mechanism. TCP/IP is designed to accommodate the diversity of physical networks, and this adaptability is mainly embodied through the IP layer. Due to the diversity of physical networks, the data frame format and address format of various physical networks are very different. In order to shield these low-level details and make the communication between networks using different physical networks, TCP/IP adopts the same form of the unified description of the physical data frame and the physical address. Such IP to the upper level to provide a unified IP datagram and unified IP address, so that a variety of physical frame and physical address of the differences in the upper layer protocol is no longer exist.

The IP Data header format is as follows:

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.