Linux C Advanced Programming-network programming (1)

Source: Internet
Author: User

Linux Advanced Programming --BSD Socket of network programming


Tenet: The learning of technology is limited and the spirit of sharing is limitless.


a Fundamentals of network communication

TCP/IP Protocol Cluster Foundation: TCP/IP is a protocol cluster because TCP/IP includes TCP, IPS, UDP, ICMP, and many other protocols. is the OSI model and the TCP/IP model, TCP/IP divides the network into 4-tier models: The application layer, the transport layer, the network layer, and the network interface layer (some books divide it into 5 layers, i.e. the network interface layer is composed of link layer and physical layer)

(1) Network interface layer: the base of the model, responsible for the transmission of the data frame is received (the frame is a separate network information transmission unit). The network interface layer places the data in the frame format on the network, or takes the frames off the network.

(2) Internet (Network layer): The interconnection protocol encapsulates packets into IP packets and runs the necessary routing algorithms to effectively find the optimal path tree for the destination host. There are four kinds of interconnection protocols:

Internet Protocol IP: Responsible for path addressing and routing of packets between host and network. At present, the main IPV4 address, IPV6 has been widely used in education network.

Address Resolution Protocol ARP: Obtaining host hardware addresses in the same physical network

Internet Control Message Protocol ICMP: sends a message and reports a delivery error on the packet

Interconnect Group Management Protocol IGMP: used to implement local multicast router reporting

(3) Transport layer: The transport Protocol provides a communication session between hosts. The choice of transmission protocol depends on the data transfer method

There are two main transport protocols:

Transmission Control Protocol TCP: Provides a reliable communication connection for the application. Ideal for transmitting large batches of data at a time, and for applications that require response.

Shadow Tiger Packet protocol UDP: provides no connection communication and does not confirm the transmission packet reliability. Suitable for transferring small amounts of data at a time (typically less than 520 bytes), reliability is done by the application layer.

(4) Application layer: The application accesses the network through this layer, mainly including the common Ftp,http,dns and Telnet protocol

Elnet: Provide remote Login service

FTP: Provides an application-level file transfer service

SMTP: e-mail protocol

SNMP: Simple Network Management Protocol

DNS: Domain name resolution Service, the protocol that images the domain name into an IP address

HTTP: Hypertext Transfer Protocol, the protocol used by the Web server

two IPv4 Protocol Basics

The IP address is logically uniquely identified in TCP/IP as a host in the network, and the host address connected to the public Web is unique, and an IP address corresponds to a single host.

Two representations of IP address: binary notation and dotted decimal notation. Each IP address consists of two parts: the network number and the host number. TCP/IP Communication process:

The transport layer and the following mechanisms are provided by the kernel, and the application layer is provided by the user process (later on how to write the application using the socket API), the application interprets the meaning of the communication data, and the transport layer and the details of the following processing communication Sends data from one computer to another by a certain path. When the application layer data is sent over the network through the protocol stack, each layer of protocol is prefixed with a data header (header), called Encapsulation (encapsulation), as shown in the encapsulation of the TCP/IP packet

Different protocol layers have different names for packets, called segments (segment) in the transport layer, called datagrams in the Network layer (datagram), and are called frames in the link layer. The data is encapsulated into a frame and sent to the transmission medium, and after reaching the destination host each layer of protocol is stripped off the corresponding header, and finally the application layer data is handed to the application for processing. In the case of two computers in the same network segment, if two computers are in different network segments, then the data travels from one computer to another to pass through one or more routers across the router communication process:

In fact, there is a physical layer below the link layer, refers to the transmission of electrical signals, such as the current Ethernet network cable (twisted pair), the use of early Ethernet coaxial cable (now mainly used for cable TV), optical fiber, etc. all belong to the concept of physical layer. The ability of the physical layer determines the maximum transmission rate, transmission distance, anti-jamming and so on. A hub is a network device that works on a physical layer and is used for twisted-pair connections and signal relays (which amplify the attenuated signal again to transmit farther).

Link Layer has Ethernet, Token Ring network standards, link layer is responsible for the driver of the network card device, frame synchronization (that is, the detection of what signal from the network cable count as the beginning of the new frame), conflict detection (if the detection of a conflict automatically re-sent), data error check and so on. A switch is a network device that works at the link layer, which can forward data frames between different link layer networks (for example, between 10 Gigabit Ethernet and Gigabit Ethernet, Ethernet and Token Ring networks), and because of the different frame formats of the different link layers, the switch will re-encapsulate the incoming packets and then forward the link layer header.

The IP protocol of the network layer is the foundation of the Internet. Hosts on the Internet are identified by IP addresses, and a large number of routers on the Internet are responsible for selecting the appropriate path to forward packets based on IP addresses, and packets from the source host on the Internet to the destination host often go through more than 10 routers. Routers are network devices that work on the third tier, and also feature switches that can forward packets between different link-layer interfaces, so routers need to remove incoming packets from the network layer and link Layer two headers and reseal them. The IP protocol does not guarantee the reliability of the transmission, the packet may be lost during transmission, and the reliability can provide support in the upper layer protocol or application.

The network layer is responsible for the transmission of the point (point-to-point) (where the "point" refers to the host or router), while the transport layer is responsible for the end-to-end (end-to-end) transmission (where the "end" refers to the source and destination hosts). The transport layer can select a TCP or UDP protocol. TCP is a connection-oriented, reliable protocol, a bit like a phone call, the two sides picked up the phone after the identity of the establishment of the connection, and then talk on the line, the word over here to ensure that the hearing, and is in accordance with the order of speech heard, said after the words hang machine disconnected. That is to say, the two sides of the TCP transport need to establish a connection, then the TCP protocol to ensure the reliability of data transmission, the loss of packets automatically re-send, the upper-level applications receive always reliable data flow, communication and close the connection. The UDP protocol is not connection-oriented, and does not guarantee reliability, a bit like sending letters, writing a letter into a mailbox, neither can guarantee that the letter will not be lost in the post process, nor guarantee that the letter is sent to the destination in order. Applications that use the UDP protocol need to do their own job of losing BAOZHONGFA, sorting messages, and so on.

After the destination host receives the packet, how to pass through each layer protocol stack finally arrives the application? The entire process as shown in the multiplexing process

The Ethernet driver first determines the payload (payload) of the data frame based on the upper-layer protocol field in the Ethernet header, which is the datagram for IP, ARP, or Rarp protocol, and is then handed to the appropriate protocol processing. In the case of an IP datagram, the IP protocol then determines whether the payload of the datagram is TCP, UDP, ICMP, or IGMP, based on the upper layer protocol field in the IP header, and is then handed over to the appropriate protocol processing. In the case of a TCP or UDP segment, the TCP or UDP protocol then determines which user process the application-tier data should be given to, based on the TCP header or UDP header's port number field. An IP address is an address that identifies a different host on the network, and the port number is the address on the same host that identifies a different process, and the IP address and port number together identify the only process in the network.

Note that although IP, ARP, and rarp datagrams all require an Ethernet driver to encapsulate frames, the functions of ARP and RARP belong to the link layer, and IP belongs to the network layer. Although the ICMP, IGMP, TCP, UDP data all need the IP protocol to encapsulate the datagram, but functionally, ICMP, IGMP and IP belong to the network layer, TCP and UDP belong to the transport layer. This article does not introduce the RARP, ICMP, IGMP protocol.

  three TCP With UDP difference

The GPRS network and CDMA network implemented by China Mobile and unicom have covered a large number of regions, and it is possible to realize data transmission through wireless network. Wireless modem using GPRS, CDMA module through China Mobile, Chinese Unicom GPRS, CDMA network data transmission, and through the TCP/IP protocol packet, can be flexible to achieve a variety of equipment access, engineering installation simple, in the application of industrial field data transmission, It can solve the problem of data transmission in remote area without network and telephone line well. With the traditional digital radio to compare, more simple, flexible, easy to operate, but also reduce costs, wireless modem transmission scheme is the best choice for modern industrial field data transmission.
At present, the data transmission bandwidth of GPRS network and CDMA network in China Mobile and Chinese unicom is about 40Kbps, and bandwidth is limited, the data acquisition scheme is best used in active alarm, Data wheel Patrol collection, alarm active callback and so on. At the same time, considering the support of the real-time acquisition scheme of the front-mounted machine, the wireless modem transmission scheme can only be supplemented by current transmission scheme. With the development of wireless communication technology, wireless transmission data bandwidth will continue to improve, using 3G wireless network, data transmission bandwidth will reach 2 m, wireless transmission scheme will gradually become the main application of monitoring transmission network. At present, due to the inherent characteristics of GPRS and CDMA, the application of GPRS and CDMA is more and more widespread in various fields, but there is much controversy about using TCP/IP protocol or UDP protocol in transmission.

TCP (transmission Control Protocol)---Transmission protocol, providing a connection-oriented, reliable byte-stream service. Before the customer and the server Exchange data with each other, a TCP connection must be established between the two parties before the data can be transferred. TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other.

UDP (Userdatagram Protocol)---User Datagram Protocol, is a simple transport layer protocol for datagram. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is very fast.

Here, let's briefly talk about the difference between TCP and UDP:
1. Connection-based and no-connection
2. Requirements for system resources (more TCP, less UDP)
3. The structure of the UDP program is relatively simple
4. Flow mode and Datagram mode
5. TCP guarantees data correctness, UDP may drop packets, TCP guarantees data order, UDP does not guarantee

In addition to the situation of the GPRS network specific talk about their differences:
1. TCP transmission has a certain delay, probably 1600MS (mobile provided), UDP response speed slightly faster.
2. TCP Header Structure
Source Port 16-bit
Destination Port 16-bit
Serial Number 32 bits
Response Number 32 bit
TCP Header Length 4 bits
Reserved6 bit
Control Code 6 Bits
Window size 16 bits
Offset 16 bits
Checksum 16-bit
Option 32-bit (optional)
This gives us the minimum size of the TCP header. That's 20 bytes.

UDP header Structure
Source Port 16-bit
Destination Port 16-bit
Length 16 bit
Checksum 16-bit
(UDP packets are much smaller. This is true. Because UDP is a non-reliable connection. The idea is to send the data packets as quickly as possible. So the UDP protocol seems very streamlined.)

3. GPRS network Port resources, UDP is very scarce, change quickly.

While TCP uses reliable link transmission, there are no port changes in the application of industrial applications generally have the following characteristics,

1. Requirements at all times transmission, but there are some occasions is the timing of transmission, in general, the entire transmission process requires the server center and GPRS terminal equipment to each other, always transfer data.
TCP itself is a reliable link transmission, providing a constant two-way transmission channel, can well meet the requirements of industrial field transmission. However, the GPRS network also has a limitation on the TCP link: This link has no data traffic for a long time (about 20 minutes or so, depending on the situation), and it automatically lowers the priority of this link until the link is forcibly disconnected. As a result, the heartbeat packet (typically a byte of data) is used to maintain the link in practice.
UDP because of its own characteristics, as well as the limitation of the GPRS network UDP port resources, after a period of time without data traffic, the port is easy to change, the impact is from the server center to the GPRS terminal to send data, GPRS terminal can not receive. The specific reason is that the mobile gateway from which to make a relay, the need to send a UDP packet to the host to maintain this IP and port number, so that the host can be active to GPRS UDP packet and I found in the test, this interval is very short, I am in more than 1 minutes to send a UDP packet to maintain enough, But longer may be mobile gateway over there will lose this port, at this time if the host want to actively send data to GPRS, that must be no, only GPRS terminal equipment to send a UDP packet in the past, mobile re-assign you a transit IP and port to be able to conduct two-way communication.

2. Requires less packet drop rates. Some industrial occasions, such as electric power, water meter reading, environmental monitoring and so on, do not allow the transmission of data loss or maximize the reliability of data requirements. From this point of view, it is obvious in the wireless data transmission process, TCP more than UDP to ensure the integrity of the data, reliability, there is a smaller packet loss rate. This is true in the actual test. Xiamen Sanrong Technology Co., Ltd. to provide GPRS terminal equipment as an example: TCP in the 9,udp of the thousand points in the 17 or so.

3. Requirements to reduce costs. At present, a large part of the application of GPRS equipment is to replace the pre-wireless digital transmission station, in addition to the scope of use, its main consideration is the cost. To cut costs is, of course, the people most willing to accept. And the cost is directly related to the flow, low flow, the cost is low. Although TCP itself is more than UDP Baotou, but UDP in practical applications often need to maintain two-way channels, it must be through a large number of heartbeat packet data to maintain the port resources. In total, the actual traffic to UDP is larger than TCP. Many users in the early days do not understand that UDP needs a large number of heartbeat packets to maintain port resources, often think of UDP more than TCP to save traffic, in fact there is a misunderstanding.

4. In some specific applications, for example, some banks of the constant interaction system, the response speed is very high, when the data transmission frequency faster, do not need a large number of heartbeat packets to maintain the UDP port resources, using UDP is more advantageous.

5. In the current 1:N transmission mode, there are more than one GPRS terminal to transmit data to a server center, UDP is much better than TCP, because UDP consumes less system resources. But in the actual application found that many users are still using TCP transmission mode, the establishment of two-level center 1:a (1:N), that is, each sub-center corresponding to n/a equipment, processing data independently, and then unified data transfer to the main center. This can not only ensure the transmission process using TCP transmission protocol, but also good processing of the central server multi-link system consumption problems.

Linux C Advanced Programming-network programming (1)

Related Article

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.