Linux Programming Learning notes----TCP/IP protocol cluster based on socket network programming

Source: Internet
Author: User
Tags class definition

Reprint please indicate the source:, Thank you!

Content Summary

This section focuses on the basis of network communication, mainly related to the content is:

TCP/IP protocol Cluster Fundamentals: two models

IPV4 Protocol Basics:IP Address Classification and representation, subnet masks, etc.

IP address translation: dotted decimal \ binary

TCP/IP protocol cluster BasicsOSI model

We know that in the computer network, there are all kinds of devices, so how to achieve the communication of these devices?

Obviously through the standard communication protocol, however, the whole network connection process is very complex, including hardware, software data packets and applications linked to each other, if you want to write a network of all the functions are connected in a piece of the program, then when a small link problem, the whole program needs to be rewritten, very troublesome!

What do you do? It doesn't matter, we can divide the entire network connection process into several strata (layer), each stratum has the special independent function, and each stratum's program code can compose independently, because each stratum's function does not interfere with each other. This way, when a problem occurs in a small section, simply re-compose the program code at that level. So the program is also easy to write, the entire network concept is clearer! That's the concept of OSI seven-layer protocol (Open System interconnection) that you hear so much about today!

If illustrated, then the correlation of these seven hierarchies is somewhat like this:

Figure 1-----Correlation of the layers of the OSI seven layer protocol

By definition, the closer the hardware class is to the bottom layer (layer 1), the closer the application is to the upper layer (layer 7). Whether it is the receiving end or the sending side, each class only knows the other person's data of the same stratum . and the entire transmission process is like people in the courier, we put the data into the seventh layer through the application of the package, and then put the seventh layer of the package in the sixth layer of the package, sequentially placed in the first layer of the largest package, and then sent out to the receiving end. The host on the receiving end will have to start with the first package, take each parcel apart in sequence, and pass it on to the corresponding responsible class. This is a feature of the OSI seven layer protocol that needs attention in terms of class definition.

Since it is a parcel, we all know that there is an important message on the surface of the parcel, including where it comes from, where it is going, who the receiver is, and so on, and the package is the real data. Similarly, in the seven layer protocol, each layer will have its own unique header data (header), telling the other party what the information is, and the real data is attached! We can use the following diagram to indicate the names of each layer of the seven layers, and how the data is placed in each layer of the package:

Figure 2-----How the OSI layer seven protocol data is delivered

Carefully look at each part of the datagram, the upper layer of the package is placed in the lower data, and the data is in front of the table header. One of the more special is the second layer , because the second layer (the Data link layer) is primarily located in the middle of the software envelope (packet) and the hardware frame (frame), he must be wrapped in software packaging into the hardware can handle the package, So this class is divided into two sub-layers in the processing of the corresponding data. Because of the special, so the second layer of data format is not the same, the end of a check code appears .

What are the tasks that each class is responsible for? Simply put, each layer is responsible for the following tasks :

Layered Responsible for content
Layer 1
Physical Layer
Physical Layer
Because the network media can only transfer 0 and 1 of this bit string, so the physical layer must define the media device used voltage and signal, but also must understand the data frame into a bit string encoding method, finally connected to the physical media and transmit/receive bit string.
Layer 2
Data Link junction layer
Data-link Layer
This layer is a very special class, because the bottom is the definition of the entity, and the upper layer is the definition of software encapsulation. So the second layer is divided into two sub-layers in the data conversion action. In the Partial Hardware Media section, the main responsibility is the Mac (Media Access Control), we call this data wrapped in a Mac frame, Mac is the network media can handle the main datagram wrap, which is eventually encoded by the physical layer into a bit string data. The MAC has to use the communication protocol to access the media, the most commonly used is the IEEE 802.3 Ethernet network protocol.

As for the software-biased part is controlled by the logical link layer (logical link Control, LLC), mainly in multitasking from the upper layer of packet data (packet) and into the format of the MAC, responsible for the work including information exchange, traffic control, error processing and so on.
Layer 3
Network layer
Network Layer
This layer is of most interest to us, as the IP (Internet Protocol) We have mentioned is defined at this level. At the same time, it also defines the online establishment, termination and maintenance of the computer, data packet transmission path selection and so on, so the most important in this level, in addition to the IP, is the packet can reach the destination route concept!
Layer 4
Transport Layer
Transport Layer
This hierarchy defines the sending and receiving end of the online technology (such as TCP, UDP technology), including the packet format of the technology, data packet delivery, process control, transmission process detection and recovery re-transmission, etc., to ensure that each data packet can be correctly arrived at the destination.
Layer 5
Meeting Layer
Session Layer
In this level, the main definition of the two-address link between the connection and hang-up, in addition, can also establish the application of the talk, provide other enhanced services such as network management, check-in and return, the control of talk and so on. If the transport layer is to determine whether the data packets can correctly reach the target, then the meeting layer is to determine the network services to establish online confirmation.
Layer 6
Presentation Layer
Presentation Layer
The data format we make on the application does not necessarily conform to the standard encoding format of the network transmission! So, at this level, the main action is to convert (or re-encode) the data format from the local application into the standard format of the network, which is then handed over to the protocol of the bottom transport layer for processing. So, at this level, the main definition is the transformation of the data format between the Network Service (or program), including the addition and decryption of the data on this layer.
Layer 7
Application Layer
Application Layer
The application layer itself is not owned by the application, but is a communication interface that defines how the application enters this layer to receive or transmit the data to the application, which is ultimately presented to the user.

In fact, the OSI seven layer protocol is only a reference model, and the current network society does not have a well-known operating system that uses the OSI seven-layer protocols for Networking program code.

TCP/IP protocol

The TCP/IP network protocol stack is divided into the application layer (application), the Transport Layer (TRANSPORT), the network layer (networks) and the link layer (link) four layer. As shown, if not specifically stated, the general quoted figure is from the "TCP/IP detailed Volume One".

Figure 3-----The TCP/IP protocol stack

In fact, TCP/IP is also the concept of using the OSI Seven layer protocol, so it also has a layered architecture, just to simplify it to four layers, the structure of the comparison is not so rigorous, the program is more easy to write.

Since TCP/IP is simplified by the OSI Seven layer protocol, is there any correlation between the two? Their relevance can be illustrated below, along with a list of communication protocols, packet formats, and related standards that are currently common under this architecture:

Figure 4-----The correlation between OSI and TCP/IP protocol

From this, we can find that TCP/IP will apply, performance, talk three layer integration into an application layer, the application layer can be implemented on the implementation of the Protocol has HTTP, SMTP, DNS and so on. The transport layer does not change, but the packet format is divided into connection-oriented TCP and non-connected UDP packet formats based on the reliability of the transmission. The network layer has not changed, the main content is to provide IP packets, and can choose the best route to reach the destination IP address. The data link layer and the physical layer are integrated into a link layer, including the definition of hardware signals, the encoding of the frame-string, and so on, so the main hardware (whether the area network or WAN) related.

How does TCP/IP work? Let's take a look at the Google portal that we often connect to, and the whole online state looks like this:

    1. Application phase: You open the browser, enter the URL column in the browser, press [Enter]. At this point, the URL column and related data will be wrapped into a browser data, and down to the TCP/IP application layer;
    2. Application layer: The HTTP communication protocol provided by the application layer, the data from the browser is reported, and given an application layer header, and then dropped to the transport layer;
    3. Transport layer: Because HTTP is reliable online, the data is dropped into a TCP packet, and a TCP packet header is given to the network layer;
    4. Network layer: The TCP package into the IP packet, and then give an IP header (mainly the source and destination IP), to the link layer dropped;
    5. Link layer: When using the Ethernet network, IP will be wrapped to the Mac frame according to CSMA/CD Standard, and give the MAC header, and then turn into a bit string, transfer media to the remote host.

Wait until Google receives your package, in the opposite direction to disassemble, and then to the corresponding level of analysis, and finally let Google's WWW server software to get the data you want, the server software according to your requirements, to obtain the correct information, and follow the above process, Wrap it up one layer at a to the end and transfer it to your computer page.

A simple explanation of the functions and protocols of each layer

1) Network interface layer: the base of the model, which is responsible for transmitting and receiving the data frame (the frame is the independent transmission unit of network information). The layer sends frames to the network or is removed from the network.

2) Interconnection layer: The interconnection protocol encapsulates the packet into an IP packet and makes the necessary routing algorithm to find the optimal path tree of the destination host. There are four kinds of protocols:

① Internet Protocol IP: Responsible for locating paths and routing packets between the host and the network.

② Address Resolution Protocol ARP: obtains the host hardware address in the same network.

③ Internet Control Information Protocol ICMP: Sending information and reporting error messages about the packet

④ Interconnect Group Management protocol IGMP: Same 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 of data transmission: There are two main transport protocols:

TCP Transmission Control Protocol: Provides a reliable communication connection for applications that are suitable for transmitting large amounts of data at one time and for applications that require response.

UDP User Packet Protocol: provides non-connected communication, Force transmission packet reliable confirmation, suitable for small amount of data and do not need to be confirmed to respond to the situation

4) Application layer: The application accesses the network through this layer, the main common protocol is ftp,http,dns,telnet.

TCP/IP protocol cluster system, in the network interface layer, the most important information is the host's MAC address, in a physically unique indicator of a host, IP layer of IP address logically uniquely identifies a host, if a host has multiple IP addresses, then it has multiple identities on the network. Inside the host, The port of the transport layer corresponds to a unique application service.

IPV4 Protocol Basics

A host that is connected to the network must have IP to communicate with other hosts

representation and classification of IP addresses

There are two representations of the 1.i P address:

Dotted decimal and dotted binary.

The decimal representation of each IP address has a length of four bytes, consisting of a 4~8 bit, often referred to as a eight-bit body. The 4 domains of an IP address indicate the network number and the host number, that is, each IP address consists of two parts: network number net_id and host number host_id.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:12PX;" >IPV4 structure: struct  in_addr {         in_addr_t  s_addr;//32-bit IPV4 address, network byte order};</span>
where network ID: Identify a physical network, all hosts on the same network use the same network number, have the same network host ID and communication between the physically connected hosts do not need a routing device, that is, they are in the same LAN.

Host ID: Identify a client \ server \ router or other TCP/IP host in the network. For the same network number, the host number is unique. Each host is identified by a logical IP address for the network number and host number.

The meaning of IP in the same domain

So how does the same domain set up, and what is the benefit of setting the IP in the same domain?

    • Net _id and host_id Restrictions:
      In the same network segment, net_id is constant, and host_id is not repeatable, in addition, host_id in the binary notation, not both 0 and 1, because all 0 represents the entire Network segment address (netwo RK IP), and all 1 represents the broadcast address (broadcast IP) .

    • in the same physical network segment of the host if you set the same domain IP range (non-repeatable), these hosts can be directly in the LAN through the CSMA/CD function of the network with the broadcast networks, that is, direct network cards can be transmitted to the NIC data (through the MAC);

    • Set the situation of different networks in the same physical network segment
      Within the same physical network segment, if the two hosts are set to a different IP network segment, the broadcast address is different, resulting in the inability to go online by means of a broadcast. It's time to communicate through a router (router) to connect two domains together.

    • size of the domain
      When the host_id occupies more bits, that is, the number of host_id, the more IP that can be used to set the host in the same domain.

Classification of 2.IP addresses

For the convenience of IP management and registration, as well as the convenience of managing the relationship between the host number and network number, InterNIC divides the entire IP segment into five levels, each of which is primarily related to the first few bits of the IP value, and is defined as follows:

the definition of the first number in the Network as a binary description:Class A:0Xxxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxxthe beginning of ==> neti_d is 0, which accounts for a byte |--net--|---------host------------|Class B:TenXxxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxxthe beginning of ==> Neti_d is 10, two bytes |------NET-------|------host------|Class C: theXxxxx.xxxxxxxx.xxxxxxxx.xxxxxxxxthe beginning of ==> Neti_d is 110, which is three bytes |-----------net-----------|-host--|Class D:1110Xxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx==> Neti_d begins with 1110, the entire address is a multicast addressClass E:1111Xxxx.xxxxxxxx.xxxxxxxx.xxxxxxxx==> Neti_d begins with 1111, the entire address is a reserved address.Five types of ratings in decimal notation:Class A:0.xx.xx.xx ~ 127.xx.xx.xxClass B:128.xx.xx.xx ~ 191.xx.xx.xxClass C:192.xx.xx.xx ~ 223.xx.xx.xxClass D:224.xx.xx.xx ~ 239.xx.xx.xxClass E:240.xx.xx.xx ~ 255.xx.xx.xx

According to the above table, we can know that you just know the first decimal number of IP, you can roughly understand the IP belongs to which level, as well as the number of IP in the same domain. However, in the table above you only need to remember three levels, that is, class A, B, C , because class D is used as a special function of the multicast (multicast) (most commonly used in a large number of computer network restore), as for the class E is reserved unused A network segment for experimentation only. Therefore, can be used to set up in the general system above, only class A, B, C three classes of IP.

A Class A address can have a large number of hosts, the highest bit 0, followed by the seven-bit marked net_id. Altogether 126 networks are allowed. 127 of the last segment is a native loopback test. Broadcast address is x.255.255.255

Class B addresses up to two positions of 10, the first 16 network numbers, and the last 6-bit host numbers, allowing 16,384 networks. Broadcast address: x.x.255.255

Class C Address: High three-bit 110, the first 24-bit network number, and the last 8-bit host number, which allows about 2 million networks. Broadcast address is x.x.x.255

When assigning network numbers and host numbers, observe the following guidelines:

    • The network number cannot be 127, which is reserved for local loops and diagnostic functions
    • The network number and host number cannot be set to 1. If each bit is 1, it will be interpreted as an in-network broadcast instead of a host number.
    • You cannot place 0, otherwise the address is interpreted as "This is the network"
    • For this network, the host number is unique, otherwise the IP address has been assigned or conflicting issues
Subnet Mask (NetMask)

A subnet mask is required for each host of TCP/IP. is a 4-byte address. Used to encapsulate or mask part of an IP address to differentiate between the network number and the host number.

In the subnet mask, the location of the corresponding IP address network number is set to 1, and the location of the corresponding host number is set to 0. That is, the network address can be computed by the IP address and subnet mask.

Another important function of the subnet mask is subnetting ..... Don't dwell on it.

IP Address TranslationThe syntax for the inet_addr () function is as follows:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>unsigned long inet_addr (const char * string);
This function uses string as the input parameter and converts the dot decimal IP address into a 32-bit binary notation, and the return value of the function is the 32-bit binary network byte order. Of course, if the string is not a valid dotted decimal IP address, the function returns Inaddr_none. It is also important to note that when the INET_ADDR function returns Inaddr_none, it does not establish a valid errno value, so when the function returns an error, do not test the value of errno.

Avoid using the INET_ADDR function in the new program, instead use the Inet_aton function instead. Because for the INET_ADDR function, even if the input parameter is a valid IP address:, his return value is still inaddr_none.

Inet_aton function

The Inet_aton function is an improved form of 32-bit IP addresses that convert IP addresses in the form of strings to network byte order. The syntax is as follows:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>int inet_aton (const char* string, struct in_addr*addr);

The parameter string represents the ASCII representation of the dotted decimal IP address. The output parameter addr is a new structure that is followed by a new IP address. A function call successfully returned a value other than 0. Failed to return 0. Of course he did not create a valid errno value.

Now let's take a look at the Inet_ntoa function

Sometimes when a user connects to your server and needs to know his IP address, the system provides the INET_NTOA function to convert the 32-bit binary IP address representation into a dotted-decimal string form:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>char* inet_ntoa (struct in_ Addr addr);

It is important to note that the return value of the INET_NTOA function is valid until the next call. So if you use Inet_ntoa in a thread, make sure that only one of the threads calls the function at a time. Otherwise, the returned structure of one thread may be overwritten by the results returned by other threads.

Inet_network function

When we need to use the network mask to the IP address of the network bit or the host bit extraction, if you can convert the dotted decimal IP address into the host byte sequence of the 32-bit binary IP address form is convenient, and the role of the Inet_network function is so, the syntax is as follows:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>unsigned long inet_network ( Const char* addr);

The input parameter of the function is a dotted decimal IP address stored in the string addr, and the return value is a 32-bit binary address of the host byte order, but if the input parameter is incorrect, the return value is 0xFFFFFFFF.

Returning results in the form of host bytes ensures that the user can safely use the netmask, because if the return value is a network byte order, the network mask and program code used by the different CPU platforms will vary.

inet_lnaof function

The function inet_lnaof function is to convert the IP address in the socket address (network byte order) to the host ID (host byte order) without network bit, this function saves us a lot of trouble, because we do not need to classify the IP address, and then extract the host from the IP address. The function syntax is as follows:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>unsigned long inet_lnaof ( struct in_addr addr);

The following table provides some typical examples and return values that you can enter as Inet_lnaof functions:

Inet_netof function

The INET_LNAOF function returns the host ID, and the Inet_netof function returns the network ID, the function syntax is as follows:

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>unsigned long Inet_netof ( struct in_addr addr);

The following table shows some examples:

inet_makeaddr function

According to the previous content we know that using the Inet_netof function and the inet_lnaof function, we can separate the host in the IP address from the network bit, and sometimes we need to merge it into a new IP address based on the extracted master camera and the network. At this point we can use the INET_MAKEADDR function.

#include <sys/socket.h> #include <neiinet/in.h> #include <arpa/inet.h>struct in_addr inet_makeaddr ( int NET, int host);

function Inet_pton

The function can be converted according to the address protocol type, which supports IPV6 conversions.

The Inet_ntop function also supports

The following is an example of IPV4 address translation:

#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <sys/socket.h>int Main (int Argc,char *argv[]) {in_addr_t net;struct in_addr net_addr,ret;char buf[128];memset ( NET=INET_ADDR (""), net_addr.s_addr=net;printf ("inet_addr ( =0x%x\n", buf), Inet_addr (""));p rintf ("Inet_network ( =0x%x\n", Inet_network ("")); printf ("Inet_ntoa (net)%s\n", Inet_ntoa (NET_ADDR)), Inet_aton ("", &ret);p rintf ("Test Inet_aton, Then Inet_ntoa (ret)%s\n ", Inet_ntoa (ret));}

#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <sys/socket.h>int Main (int Argc,char *argv[]) {in_addr_t net;struct in_addr net_addr,ret;char Buf[128];inet_pton (Af_inet, "", &ret), Inet_ntop (af_inet,&ret,buf,128);p rintf ("buf=%s\n", buf);}

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: 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.