Linux network programming | network programming basics

Network programming foundation

1. Socket Overview

1.1 Socket definition

Socket is a special I/O interface and also a file descriptor. Socket is a commonly used communication mechanism between processes. It can not only realize the communication between different local processes, but also communicate between the processes of different hosts through the network.
For network communication, each Socket can have a network address structure (Protocol, local address, local port). Socket is created by a special function, and returns an integer Socket descriptor, all subsequent operations are implemented through the Socket descriptor

1.2 Socket type

There are three common Socket types as follows:

  • Streaming socket (SOCK_STREAM): Provides a reliable, connection-oriented communication stream to ensure the reliability of data transmission and orderly sending and receiving. TCP communication uses stream sockets
  • Datagram socket (SOCK_DGRAM): It realizes an unreliable, connectionless service. Data is transmitted through independent messages, which is disorderly and does not guarantee reliable transmission. UDP communication uses this socket word
  • Raw socket (SOCK_RAW): allows direct access to the underlying protocol (such as IP or ICMP), it is powerful but inconvenient to use, and is mainly used for the development of some protocols

2. IP address

2.1 The role of IP address

The IP address is used to identify a host in the network. According to different protocol versions, it is divided into IPv4 (32-bit) and IPv6 (128-bit). An IP address consists of two parts: network number and host number. The network number and host number are distinguished according to the subnet mask. Simply put, with the source IP and destination IP, data packets can be transmitted between different hosts

2.2 IP address format conversion

There are two different formats for IP addresses: point format and 32-bit binary format. The former is familiar to users, and the latter is the storage method of IP addresses in network transmission.
IP address conversion function:

  • Convert decimal dotted form to binary form: inet_addr(), inet_pton() (IPv4/IPv6 compatible)
  • Binary form is converted to decimal form: inet_ntop() (IPv4/IPv6 compatible)
/*****十进制点分形式转换为二进制形式*****/
/***inet_addr()函数***/
函数原型:int inet_addr(const char *strptr)
传 入 值:strptr 要转换的IP地址字符串
返 回 值:成功 返回32位二进制IP地址(网络字节序)
		 失败 返回-1
		 
/***inet_pton()函数***/
函数原型:int inet_pton(int family, const char *src, void *dst)
传 入 值:family --> AF_INET: IPv4协议
				--> AF_INET6: IPv6协议
		 src 要转换的IP地址字符串
		 dst 存放转换后的地址的缓冲区 
返 回 值:成功返回0;失败返回-1

/*****二进制形式转换为十进制点分形式*****/
/***inet_ntop()函数***/
函数原型:int inet_ntop(int family, void *src, char *dst, size_t len)
传 入 值:family --> AF_INET: IPv4协议
				--> AF_INET6: IPv6协议
		 src 要转换的二进制IP地址
		 dst 存放十进制地址字符串的缓冲区 
返 回 值:成功返回 dst;失败返回 NULL
2.3 Address structure related processing

The data structures sockaddr and sockaddr_in are both used to represent address information, usually sockaddr_in is used to store a certain network address, which is forced to be converted into a sockaddr type pointer when in use, and its definition is as follows:

/*****sockaddr*****/
struct sockaddr{
    
    
	unsigned short sa_family;	//地址族
	char sa_data[14];			//14字节的协议地址
}

/*****sockaddr_in*****/
struct sockaddr_in{
    
    
	short int sin_family;			//地址族
	unsigned short int sin_port;	//端口号
	struct in_addr sin_addr;		//IP地址
	unsigned char sin_zero[8];		//填充0以保持与sockaddr同样大小	
}
/***其中的struct in_addr结构体***/
struct in_addr{
    
    
	uint32_t s_addr;
}

Common optional values ​​for the sa_family field

head File #include <netinet/in.h>
sa_family AF_INET: IPv4 protocol
AF_INET6: IPv6 protocol
AF_LOCAL: UNIX domain protocol
AF_LINK: Link address protocol
AF_KEY: key socket

3. Port

The port (number) is an unsigned short integer, the value range is from 0 to 65535; the port number is a resource of the system, the port number in the range of 0 to 1023 is generally used by system programs; TCP port number and UDP port The numbers are independent and do not affect each other; if the IP address can represent a host in the network, the port number can be used to represent a socket inside the host. That is, when a socket is created, it needs to be bound to a certain IP address and port number, so that both parties can achieve end-to-end communication

4. Endianness

Endianness is also known as Host Byte Order (HBO), which refers to the storage method of multi-byte integer data in a computer. There are two kinds of endianness: big endian (high byte is stored in low address, low byte is stored in high address) and little endian (contrary to big endian, PC usually adopts little endian mode). In network communication, the sender and receiver may use different endianness. In order to ensure that the data can be correctly parsed and processed after receiving, it is uniformly stipulated that the data is transmitted on the network in the order of high byte priority (network endianness) . Therefore, the data needs to be converted between host byte order and network byte order before sending and after receiving. The
byte order conversion includes 4 functions: htons(), ntohs(), htonl() and ntohl(). h stands for host, n stands for network, s stands for short, and l stands for long. Usually the port number of 16Bits is processed by the first two functions, and the IP address is converted by the latter two functions.

/*****字节序转换函数*****/
函数原型:uint16_t htons(uint16_t hostshort);
		 uint32_t htonl(uint32_t hostlong);
		 uint16_t ntohs(uint16_t netshort);
		 uint32_t ntohl(uint32_t netlong);
传 入 值:hostshort 主机字节序的16位数据 
		 hostlong 主机字节序的32位数据
		 netshort 网络字节序的16位数据
		 netlong 网络字节序的32位数据
返 回 值:成功 返回转换字节序后的数值;失败 返回-1

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108422947