The unit number is the same as that on the UNP, saving trouble.
3.2 sets of interface address Structure
In <netinet/in. h>, the IPv4 address structure is defined.
Struct in_addr {
In_addr_t s_addr; // 32B
};
Struct sockaddr_in {
Uint8_t sin_len;
Sa_family_t sa_family;
In_port_t sin_port;
Struct in_addr sin_addr;
Char sin_zero [8];
};
General interface address Structure
The address structure of the set interface is always transmitted by pointer, so the data type of this pointer is the common set interface type.
Struct sockaddr {
Uint8_t sa_len;
Sa_family_t sa_family;
Char sa_data [14];
};
This also enables the set of interface functions to be converted into pointers that only require the general set of interface address structures.
BIND (sockfd, (struct sockaddr *) & servaddr, sizeof (servaddr ));
3.3 value-result Parameter
The length of the interface address structure is transmitted as a parameter. The transmission mode depends on the Transmission Direction of the structure: From the process to the kernel, or from the kernel to the process.
1 Process -- Kernel
BIND, connect, sendto. One Parameter of the three functions is just a pointer to the interface address structure, and the other parameter is the size of the structure.
Since the size of the pointer and pointer structure are transmitted to the kernel, we can see the data length from the process to the kernel.
2 kernel -- Processes
Accept, recvfrom, getsockname, getpeername
One Parameter of the four functions is just a pointer to the interface address, and the other parameter is a pointer to the size of the self-phase structure.
Why is the structure size given to a pointer pointing to an integer? When a function is called, the structure size is a value (value, indicating the size of the kernel structure). When returned, the structure size is a result (result, telling the process, how much information is stored by the kernel in this structure ). This parameter is called a value-result parameter.
User process Kernel
---------------- Value
Structure Length
---------------- Results
Set interface address structure ------------ Protocol address
3.4 byte sorting Function
Low (byte) Bottom (memory address) is small (end)
Base-high
(Host byte order) Linux uses small ends, while Solaris, Aix, MacOS, freebsd5, and HPUX are all large ends,
(Network byte order) uses the big end.
Therefore, it is necessary to convert HBO and nbo.
# Include <netinet/in. h>
Uint16_t htons (host)
Uint32_t htonl (host)
// HBO-nbo
Uint16_t ntohs (network)
Uint32_t ntohl (network)
// Nbo-> HBO
3.5-byte manipulation Function
For data such as IP addresses, there is 0 in the middle, but there is no C string and the strxxx function cannot be used.
However, there are two types of processing functions with the same functions.
1 Berkeley Function
# Include <strings. h>
Void bzero (void * DEST, size_t nbytes );
Void bcopy (const void * SRC, void * DEST, size_t nbytes );
Int bcmp (const void * ptr1, const void * ptr2, size_t nbytes)
// The const void * PTR function can only read but cannot modify the memory unit indicated by the pointer
2 ansi c functions
# Include <string. h>
Void * memset (void * DEST, int C, size_t nbytes );
Void * memcpy (void * DEST, const void * SRC, size_t nbytes );
Int memcmp (const void * ptr1, const void * ptr2, size_t nbytes );
// The memory is that the length is used as the last parameter.
3.6-byte Conversion Function
In the binary value of the ASCII string (192.168.1.45) <--> nbo
P: Presentation (ASCII)
N: Numeric (network)
# Include <ARPA/inet. h>
Int inet_ton (INT family, const char * strptr, void * addrptr );
1-success, 0-input error
// Strptr-> addrptr
Const char * inet_ntop (INT family, const void * addrptr, char * strptr, size_t Len );
// Addrptr-> strptr
Set Len to 16 (4*4) in IPv4)