Linux Network Programming Fundamentals ---- summary

"Linux" I feel it is only literally feeling is not very simple, a thousand miles begins with one step, then we have to start learning about Linux network programming of the. The more I write a blog to record their own learning knowledge point, there are many places there may be some problems, after all, low level, please understand.
Today saw a long time Linux-related books, with a more general understanding of Linux. According to know almost in the recommendation:
Here Insert Picture Description
on the basic knowledge of Linux, he is also learning the knowledge of others summarized, summed it more comprehensive personal feeling:
the programmer must have! Very wide Linux fundamentals point, you know how much?

Internet contributed to the generation of TCP / IP protocol

  • Early use of ARPAnet network control protocol (Network Control Protocol NCP), can not be interconnected different types of computers and different types of operating systems, there is no correction.
  • There are 1973 Robert Kahn and Vinton Cerf and they work for the ARPAnet developed a new interconnection agreement.
  • December 1974 was published two first TCP protocol in detail, but once the agreement is not valid when there is packet loss correction

TCP protocol is divided into two different protocols
used to detect errors in the network transmission control protocol TCP number of cycles (the reliable transmission)
responsible for the interconnection of different network interconnection protocol IP (unreliable transport)

Since then, TCP / IP protocol birth

What is TCP / IP protocol it?

Between your computer and network equipment if you want to communicate with each other, the two sides have, such as how to detect based on the same method to communicate goals. First of which side initiated communication, the use of which language to communicate, how to end the regular communication needs to be determined in advance. different hardware, communication between the operating system, all of which requires a rule. and we will be such a rule called protocol (protocol).

In other words, TCP / IP protocol suite is all kinds of Internet-related general.

Network architecture

  • Network design method of the divide and conquer, the network is divided into different functional modules to form a layered combined organic
  • Each different functions, the internal implementation is transparent to the outside for other levels. Each layer provides services to an upper layer, the lower layer while using the service provided
  • Network Architecture refers to the hierarchy of network protocols used and each set of
  • Two types of very important architectures: OSI and TCP / IP

OSI Open Systems Interconnection model

OSI model relevant agreement has been rarely used, but the model itself is very common
OSI model is an idealized model, not yet complete implementation of
the OSI model has seven layers:
Here Insert Picture Description

In fact, TCP / IP internet protocol is the de facto industry standard:
a total of four layers:
Here Insert Picture Description
Here Insert Picture Description
As to why the layered it?
After the TCP / IP protocol layering, if need somewhere late design changes, then there is no need to replace all, only need to change layer needs to be changed. And from the designs, it will be even more simple. In applications on the application layer can only consider their assigned tasks without need to figure out what the other party was engaged in transmission in which place

Common agreement

- a network interface physical layer

MAC Address: 48 as the world's only used to identify the identity of network devices. Network interface with the physical layer of the protocol is mainly: ARP / RARP.
The ARP: mainly by its IP address to find the MAC address;
the RARP: find the IP address corresponding to the MAC address
PPP protocol: He is a dial-up protocol (3G, 4G, 5G, GPS )

- network layer (IP layer)

IP protocol: Internet protocal (divided IPV4 and IPV6)
ICMP: Internet Control Management Protocol (ping command)
IGMP: Internet Group Management Protocol (broadcast, multicast, etc.)

- Transport Layer

TCP:(Transfer Control protocol, Transmission Control Protocol) connection object-oriented, reliable data transmission one protocol. ( Data is correct, no loss of data, data disorder, communication data without duplication of arrival)
UDP:(user Datagram Protocol, User Datagram Protocol): provides unreliable, connectionless transport protocol effort ( unreliable connectionless protocol, before data transmission, because no connection can be performed with high efficiency data transmission )
the SCTP: (transmission control protocol Stram: stream control transmission protocol), TCP enhanced version offers, many-to-many, or reliable data transfer protocol connection-oriented

TCP and UDP
TCP and UDP are transport layer protocols, but both have different effects, but also has different application scenarios.
Here Insert Picture Description
Message-oriented:
transmission for the application layer packets to the UDP packet long, long UDP packet transmission, i.e. a packet transmission time. Therefore, the application must select the appropriate size of the packet.

Byte stream oriented:
Although TCP and application interaction is a first data block (sizes), but the TCP byte stream application to be a series of unstructured. TCP has a cushion, when the program data block to be transmitted is too long, it can be divided into TCP shorter re-transmission.
About TCP three-way handshake with the four waving:
The first handshake: connection is established. The client sends a connection request segment, and syn (flag bits) is set to 1, Squence Number (packet number) (SEQ) is x, the server waits for the next acknowledgment, the client enters the SYN_SENT state (connection request);

The second handshake: server receives SYN segment client segment of SYN packets to confirm settings ack (acknowledgment number) to x + 1 (ie seq + 1; at the same time he would also like to send SYN request information, SYN set 1, seq to y. All of the above information the server into the SYN + ACK segment, be sent to the client, into the server at this time SYN_RECV state.

SYN_RECV means, the passive open end of the service, the client receives and transmits SYN state when the ACK. Further receiving the ACK to the client enters the ESTABLISHED state.

Third handshake: the client receives the server SYN + ACK packet segment (Acknowledge); then setting + 1 ACK, ACK segment is sent y to the server, after the segment has been sent, the client and server have entered ESTABLISHED (successful connection) state to complete the TCP three-way handshake.

The above explanation may be a bit difficult to understand, an illustration image "graphic HTTP" in the interpretation of the above process.
Here Insert Picture Description
When the client and server via TCP three-way handshake to establish a connection later, when the data transfer is complete, disconnect on the need for TCP four times and waved. Four wave which follows:

The first wave

Seq client settings and ACK, sends a server (termination) segment FIN. In this case, the client enters FIN_WAIT_1 state, means that the client has no data to send to the server.

The second wave

Server receives a FIN segment sent by the client, the client back an ACK segment.

Third Wave

The server sends the client a FIN segment, close the connection requests, while the server enters LAST_ACK state.

Fourth Wave

The client receives a FIN segment sent by the server, sends an ACK segment to the server, and the client enters the TIME_WAIT state. After the server receives an ACK segment client connection is closed. In this case, the client waits 2MSL (fragment refers to a maximum survival time in the network) after still not received a reply, then the server has been shut down properly, so that the client can close the connection.
The complete process is as follows:
Here Insert Picture Description

- Application Layer

Network access protocols: HTTP / HTTPS
message transmitting and receiving protocol: POP3 (closed) / SMTP (hair), IMAP (acceptable part of the message), the FTP
the Telnet / the SSH: Telnet
embedded relevant:
the NTP: Network Time Protocol
SNMP: Simple network management protocol (centralized management of network devices)
the RTP / the RTSP: a protocol for transmitting audio (security monitoring)

TCP / IP protocol communication model

Here Insert Picture Description

Network packet refers to a packet data transmission protocol specified by the network.
Computers can only binary data, the binary data is stored in a computer. To enable communication between multiple computers, it must rely on a predetermined communication protocol, such as TCP / IP / HTTP network protocol. In order to distinguish each protocol, a protocol data during transmission, is used together with the specified format. It can also be understood as a transmission of data packed. For example: courier what you want to buy your home, we must first packaged and then add your personal information, and finally to your hands, the whole process is called packet.

Here Insert Picture Description

Network programming prior knowledge

Socket Description:

  • It is a programming interface
  • It is aSpecial file descriptor (everything in Unix is ​​a file)
    IO function performs its operations, such as: read (), write (), close () function is not limited to operations such as TCP / IP protocol.
  • Socket represents a resource network programming
  • Connection (Transmission Control Protocol-TCP / IP) for
  • No connection (User Datagram Protocol-UDP, and Inter-network Packet Exchange-IPX)

Socket type:

Nested word stream (SOCK_STREAM): (Unique to the TCP)
Providing a connection-oriented, reliable data transmission services, data error-free transmission and reception is repeated by the transmission order. Setting the flow control, to avoid flooding slow data recipient. It is seen as a stream of bytes of data, no length limit.
Nested word datagram (SOCK_DGRAM): The only corresponds to the UDP
Provides connectionless service. Data packets form a separate data packet is transmitted, to ensure error-free is not available, data may be lost or repeated transmission order may be received out of sequence
of the original word nested (SOCK_RAM): (Corresponding to a plurality of protocolsTransmitting the transport layer penetration)
can be prepared as IP, ICMP direct access to the lower level protocol

IP addresses

IP address is a host identifier the Internet
- the Internet host to the communication with the other machine must have an IP address
- IP Address is 32 bits (the IPV4) or 128 (IPV6)
P)
- each packet must carry the object IP address and source IP address, the router the packet is to rely on this information

  • Representation IPV4: common dotted form, such as 202.38.64.10, and finally are converted to a 32-bit unsigned integer
  • Category IP address
    the IPV4: 32-bit integer format represented
    IPV6: using a 128-bit integer to represent
    mobileIPV6: local IP (local registration), room IP (I roaming
    particular IP Address:
    LAN: 192.xxx.xxx.xxx 10.xxx.xxx.xxx
    broadcast IP: xxx.xxx.xxx.255,255.255.255.255 (network-wide broadcast)
    multicast IP: 224.xxx.xxx.xxx ~ 239.xxx.xxx.xxx

The port number

  • In order to distinguish a host receives a packet which should be transferred to the task for processing, use port numbers to distinguish
  • TCP port number and UDP port independence
  • Port number generally consists of IANA (Internet Assigned Numbers Authority) management
  • Known Port: 2023 ~ 1 (1 ~ 255 is well-known port, the port is generally 256 to 1023 occupied by the UNIX system) [FTP: 21, SSH: 22, HTTP: 80, HTTPS: 469]
  • Registration port: 1024 ~ 49150
  • Dynamic or private port: 49151 to 65535

Endian

Different types of the host CPU, the memory storing a plurality of two-byte integer sequence methods, host byte order (HBO):

  • Little-endian (little-endian) - low order byte stored in low address
    • The low byte is stored in the start address, referred to as "little-Endian" byte order, Inter, AMD, etc.
  • Big endian (big-endian) - high order byte stored in low address
    • The start address is stored in the high byte, called "big-Endian" endianness used by the ARM, Motorolo etc.
  • Data transmission network must in a grid that is big-endian byte order
  • Pc on most machines, the current application process Soket into an integer, the grid needs to be translated into a byte sequence: when application process integer removed from socket, to be converted to little endian

IP addresses used conversion function

in_addr_t inet_addr(const char *cp):
cp: IP address in dotted form, the result is 32-bit integer (contains internal endian conversion, the default mode network byte order)
characteristics:
① applies only to the IPV4
② When an error, returns -1
③ this a function not for converting 255.25.255.255
inet_pton () / inet ntop ():

#include <arpa/inet.h>
int inet_pton(int af,const char*src,void *dat)

Features:
① suitable for IPV4 and IPV6
② correctly handling the conversion issues 255.255.255.255
parameters:
①af: address protocol family (AF_INET or INET6)
②src: is a pointer (dotted fill in the form of an IP address [mainly IPv4])
③dst: result of the conversion to the dst
Note:
the rETURN of VALUE
inet_pton () return1 on success(network address was successfully converted).

Published 28 original articles · won praise 5 · Views 3721

Guess you like

Origin blog.csdn.net/qq_42711899/article/details/104374465