Linux C Advanced Programming-Fundamentals of Network programming (1)

Source: Internet
Author: User

Linux Advanced Programming --BSD Socket of network programming


Purpose: The learning of technology is limited, the spirit of sharing is unlimited.


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 comparison between 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 consists of a link layer and a physical layer)

(1) Network interface layer: the base of the model. Responsible for sending 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 execute the necessary routing algorithm, effectively find the optimal path tree to achieve 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 report 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 way data is transmitted

There are two main types of 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 incorrect delivery of the package for reliability confirmation. Suitable for transferring small amounts of data at once (typically less than 520 bytes). Reliability is completed by the application layer.

(4) Application layer: The application passes this level of access to the network. Mainly includes common Ftp,http,dns and Telnet protocols.

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, protocol used by webserver

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, with an IP address corresponding to a 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 its underlying mechanism are provided by the kernel, and the application layer is provided by the user process (the following describes how to write an application using the socket API). The application interprets the meaning of the communication data. The transport layer and the details of the communication below are processed to send data from one computer to another through a certain path to a computer.

When application-layer data is sent over the network through the protocol stack. Each layer of protocol is prefixed with a header, called Encapsulation (encapsulation), such as the encapsulation of the TCP/IP packets seen


Different protocol layers have different titles for packets. In the transport layer is called segment (segment). The network layer is called a datagram (datagram). The link layer is called a frame. 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. The two computers in the same network segment, assuming that the two computers in different network segments, then the data from one computer to another computer transfer process through one or more routers cross-router communication process:

In fact, there are physical layers underneath the link layer. Refers to the transmission of electrical signals, such as today's Ethernet cable (twisted pair), the use of early Ethernet coaxial cable (now mainly used for cable TV), fiber, etc. are 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 and other standards. The link layer is responsible for the driver of the network card device, frame synchronization (that is, from the network cable to detect what signal counts as the beginning of the new frame), conflict detection (assuming that the detection to conflict on their own initiative to re-send), data error checking and other work. A switch is a network device that works at the link layer, capable of forwarding data frames between different link layer networks (such as between 10 Gigabit Ethernet and Gigabit Ethernet, Ethernet and Token Ring networks), because the frame formats of different link layers are different. The switch is going to remove the incoming packet and then forward it again after the link layer header is encapsulated.

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 right 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. At the same time, the function of the switch is capable of forwarding packets between different link-layer interfaces, so the router needs to take the incoming packets off the network layer and the link Layer two-layer header and package it again. 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 transfer of point-to (point-to-point) (where "point" refers to the host or router). The transport layer is responsible for the end-to-end (end-to-end) transmission (where the "end" refers to the source host and destination host). 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 a connection. Then you can talk. There's something here that's guaranteed to be heard. And is heard in the order of speech, said after the hanging machine disconnected.

That is to say, the two sides of the TCP transmission need to establish the connection first, then the TCP protocol guarantees the reliability of data receiving. The lost packets are proactively re-sent, and the upper-level application receives always reliable traffic, which closes the connection after the communication. 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 complete their own lost BAOZHONGFA, message sequencing, and so on.

After the destination host receives the packet, how does it pass through the layer protocol stack to reach the application finally? The entire step, for example, with the multiplexing process seen

The Ethernet driver first determines the payload of the data frame based on the "Upper layer protocol" field in the Ethernet header (payload, the data that is actually transmitted outside the Protocol header) is the datagram of the IP, ARP, or RARP protocol, which is then handed to the corresponding 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 corresponding 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 the Port number field of the UDP header. The IP address is the address that identifies the different hosts on the network, and the port number is the address that identifies the different processes on the same host, and the IP address and port number together identify the only processes in the network.

Note that although the IP, ARP, and rarp datagrams all require an Ethernet driver to encapsulate the frames, the functions are divided, 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 the UDP the 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 transmit data via wireless network. Wireless modem adoption of GPRS, CDMA module through China Mobile, Unicom GPRS, CDMA network transmission data, and through the TCP/IP protocol data packet, flexible to achieve a variety of device access, project installation is simple, in the industrial field transmission of data applications, It is very good to solve the problem of transmitting data in remote area without network and telephone line.

Compare with the traditional radio station. More simple, flexible, easy to operate, at the same time also reduce costs, wireless modem transmission program is a modern industrial field transfer of data the best choice.
At present, the transmission data bandwidth of GPRS network and CDMA network in China Mobile and Chinese unicom is around 40Kbps, and the data collection scheme is best used for active alarm, Data wheel Patrol collection, alarm active callback and so on, with less bandwidth consumption. At the same time, consider the support of the front-mounted real-time collection scheme. The Wireless modem transmission scheme can only be used as a supplement to the current transmission scheme. With the continuous development of wireless communication technology, wireless transmission data bandwidth will continue to improve, adopt 3G wireless network, transmission data bandwidth will reach 2 m, wireless transmission scheme will gradually become the main application of monitoring transmission network. Right now, because of the inherent characteristics of GPRS and CDMA. The applications of GPRS and CDMA are more and more extensive in various fields. However, there is a lot of controversy about whether the TCP/IP protocol or the UDP protocol is used in transmission.

TCP (transmission Control Protocol)---Transmission protocol, providing a connection-oriented, reliable byte-stream service. Before the client and server Exchange data with each other. A TCP connection must be established between the two parties before the ability to transfer data. TCP provides a time-out to re-send, discarding repeated data. Test data, flow control and other functions to ensure that data can be transmitted from one end to another.

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 the data is transmitted. And there is no time-out for the re-send mechanism. The transfer speed is therefore 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 and UDP may drop packets. TCP guarantees data order, UDP does not guarantee

In addition to the situation of the GPRS network to discuss their differences in detail:
1.

TCP transmission has a certain delay. About 1600MS (mobile provided), UDP responds slightly faster.
2. TCP Header Structure
SOURCE Port16 Bit
Destination Port 16 bit
Serial Number 32 bits
Response Number 32 bit
TCP Header Length 4 bits
Reserved6 bit
Control Code 6 Bits
Form size 16-bit
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 Port16 Bit
Purpose port16 Bit
Length 16 bit
Checksum 16-bit
(UDP has a very small packet.) 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 looks very streamlined.

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

TCP is transmitted using a reliable link. There is no problem with the port change application in industrial applications generally has the following characteristics,

1.

Requirements are always transmitted, but there are some occasions are timed transmission, in general, the entire transmission process requires the server Center side and GPRS terminal equipment can be mutual, time-to-time data transmission.
TCP itself is a reliable link transfer. Provides an always-on, bidirectional transmission channel. Can be very good to meet the requirements of industrial field transmission. However, the GPRS network also has a limit on the TCP link: this link in a long time (about 20 minutes, depending on the details) No data traffic, will voluntarily reduce the priority of this link until the link is forcibly disconnected. So in practice, a heartbeat packet (usually a byte of data) is used to maintain the link.
UDP because of its own characteristics, as well as the limited GPRS network Udpport resources, after a period of time without data traffic, porteasy change. The effect is to send data from the server center to GPRS terminal, GPRS terminal cannot receive. The reason for this is that the mobile gateway is making a relay from it. Need a certain time to the host to send a UDP packet to maintain this IP and port number, so that the host can actively give GPRS UDP packets and I found in the test, this interval is very short, I in more than 1 minutes to send a UDP packet can be maintained, but longer may be mobile gateway over there will be lost this port , it is assumed that the host to actively send data to GPRS, it is certainly not possible, only GPRS terminal equipment to send a UDP packet in the past, the move again to assign you a transit IP and port, before you can conduct two-way communication.

2. Requires less packet drop rates. Some industrial occasions, such as electricity, water meter reading, environmental monitoring and so on. Data loss during transmission or maximum data reliability is not tolerated.

From this point of view, it is very obvious that in the process of transmitting data wirelessly, TCP can guarantee the integrality and reliability of data more than UDP, and there is a smaller packet loss rate. The same 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 very large number of applications of GPRS devices are to replace the pre-wireless digital transmission station, in addition to the use of the range. The main consideration is the cost. To cut costs is, of course, the most acceptable to all. 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 port resources. Overall, the actual traffic to UDP is larger than TCP. Very many users in the early days do not understand that UDP needs a large number of heartbeat packets to maintain port resources, often feel that UDP is more than TCP to save traffic, in fact there is a misunderstanding.

4.

In some specific application situations. For example, some banks of the constant interaction system, the response speed is very high, at this time the transmission of data frequency faster, do not need a large number of heartbeat packets to maintain udpport resources. The use of UDP is more advantageous.

5. In the current 1:N transmission mode, there are multiple GPRS terminal equipment to a server center data transmission. Using UDP at this point is much better than TCP, because UDP consumes less system resources.

However, in the practical application, it is found that very many users or TCP transmission mode, the establishment of two-level center 1:a (1:N), that is, each sub-center corresponding N/a equipment, processing data independently, and then unified data transfer to the main center. This ensures that the transmission protocol of TCP is used in the transmission process. It also has the advantage of having a central server multi-link system consumption problem.

Linux C Advanced Programming-Fundamentals of 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.