Reprinted from: Forever Unixttp: //fanqiang.chinaunix.net/index.shtml
Set of interface programming foundations
This article comes from: http: //sunsland.top263.net Author: (2001-10-22 10:00:00)
IPv4 socket address structure struct in_address {IN_ADDR_T S_ADDR;} // 32bits IPv4 Address // Network Byte Sequence NetWork Byte Order Struct SockAddr_in {Unit8_t Sin_len; // Length Member, No need to set sa_family_t sin_family; // cover interface structure address family, AF_INET IN_PORT_T SIN_PORT; / / 16-bit TCP or UDP port number struct in_addr sin_addr; // 32-bit TCP or UDP port address char SIN_ZERO [8]; // unused} // in: Internet // s: socket --- -------------------------------------------------- -------------------------- General Socket Interface Address Structure Set Interface address structure is only used on a given host. Some members (IP addresses and port numbers) in the structure are used in communication between different hosts, but the structure itself does not participate in communication. When it is passed to any set of socket functions as a parameter, the socket address structure always passes the pointer. To pass, but through the pointer to get the socket function of this parameter must handle the socket address structure from the supported protocol family. Universal Set Structure Struct SockAddr {uint8_t sa_len; sa_family_t sa_family; char sa_data [14]}; general purpose The purpose of the socket address structure: gives the pointer conversion type that pointing the address structure of the protocol. -------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- -> Pointer to the universal socket address structure. Function prototype: int Connect (int, struct sockaddr *, socklen_t) .............. struct sockaddr-in servaddr; ................ .. Connect (Sockfd, Sturct Sockaddr *) & Servaddr, Sizeof (Servaddr)); ..................... ---------- -------------------------------------------------- ------------------- byte sort function a 16-bit integer, which consists of 2 bytes.
There are two ways to store these two bytes in memory: small-end word sequence: low sequence byte stored in the start address large-end character sequence: High order byte stores in the start address Internet protocol must specify a network word NetWork Byte Order host word sequence and network word sequencing conversion function: #include