Network Programming Sockets (1) - Preliminary Knowledge

  • Know the IP address

There are two versions of the IP protocol: IPv4 and IPv6. In our subsequent blogs, all references to the IP protocol, without special instructions, refer to IPv4 by default.

An IP address is an address used in the IP protocol to identify different hosts on a network .

For IPv4, an IP address is a 4-byte, 32-bit integer.

IP addresses are usually represented as "dotted decimal" strings, such as 192.168.3.57. Each number separated by a dot represents a number, ranging from 0 to 255.

  • source IP address and destination IP address

In the IP packet header, there are two IP addresses, called source IP address and destination IP address.

  • The port number

The port number (port) is the content of the transport layer protocol.

The port number is a 2-byte 16-bit integer.

The port number is used to identify a unique process on this host.

The IP address + port number are collectively referred to as a socket, which can identify a certain process of a certain host on the network.

A port number can only be occupied by one process.

  • "Port Number" and "Process ID"

When learning system programming, we learned that pid represents a unique process, and here the port number also uniquely represents a process. What is the relationship between the two?

A process can bind multiple port numbers, but a port number cannot be bound by multiple processes.

  • source port number and destination port number

There are two port numbers in the data segment of the transport layer protocol (TCP and UDP), which are the source port number and the destination port number, which are describing "who sent the data and to whom".

  • TCP protocol

TCP (Transmission Control Protocol) means Transmission Control Protocol.

Features of TCP:

        (1) Transport layer protocol

        (2) There is a connection

        (3) Reliable transmission

        (4) Oriented to byte stream

  • UDP protocol

UDP (User Datagram Protocol) means User Datagram Protocol.

Features of UDP:

        (1) Transport layer protocol

        (2) No connection

        (3) Unreliable transmission

        (4) Datagram-oriented

The basic unit of UDP transmission and reception is the data packet.

Here we need to think about a question: since the TCP protocol can guarantee the reliability of transmission, why use the UDP protocol? What is the significance of the UDP protocol? We need to know that existence is reasonable. Although UDP cannot guarantee the reliability of transmission, its advantage is that the transmission speed is fast.

  • network byte order

The multi-byte data in the memory is divided into big and small ends relative to the memory address, and the multi-byte data in the disk file is also divided into big and small ends relative to the offset address in the file. Similarly, network data streams are also divided into big and small ends.

The sending host usually sends the data in the sending buffer in the order of memory addresses from low to high; the receiving host stores the bytes received from the network in the receiving buffer in sequence, which is also in the order of memory addresses from low to high. save. Therefore, the address of the network data flow should be specified as follows: the data sent first is the low address, and the data sent later is the high address.

The TCP/IP protocol stipulates that the network data stream should be in big-endian byte order, that is, low-address high-byte.

Regardless of whether the host is big-endian or little-endian, it will send/receive data according to the network byte order specified by TCP/IP.

If the current sending host is little-endian, you need to convert the data to big-endian first, otherwise ignore it and send it directly.

                    

In order to make the network program portable, so that the same C code can run normally after compiling on big-endian and little-endian computers, the following library functions can be called to convert the network byte order and host byte order.

                

h represents host, n represents network; l represents 32-bit long integer, s represents 16-bit short integer.

For example: htonl means to convert a 32-bit long integer from host byte order to network byte order.

If the host is in little-endian byte order, these functions convert the parameters to the corresponding big-endian byte order and return them; if the host is in big-endian byte order, the functions do not do any conversion and return the parameters as they are.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325574283&siteId=291194637