Set of interface programming foundations from: Forever Unix

xiaoxiao2021-03-06  42

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 Unit16_t htons (uint16_t host16bitvalue); Unit32_t htons (uint32_t host32bitvalue); Unit16_t NtoHS (uint16_t net16bitvalue); Unit32_t ntohs (uint32_t net32bitvalue); H: host N: NetWork S: Short (16 bits) L: long (32 bits) ----------------------- -------------------------------------------------- ----- Byte / byte flow gynen, Void Bzero (void * dest, size_t nbytes); // Clear void bcopy (const void * src, void * dest, siz_t nbytes); int bcmp (Const) Void * ptr1, const void * ptr2, size_t nbytes); return: 0-- equal, non-0 - no equal VOID * MEMSET (Void * Dest, Int C, size_t len); void * Memcpy (void * Dest, Const Void * src, size_t nbytes); IT Memcmp (const void * ptr1, const void * ptr2, size_t nbytes); return: 0-- same, non-0-, unlimited word, write write function SSIZE_T READN (int filedes, Void * buff, size_t nbytes; ssize_t writen (int filedes, const void * buff, size_t nbytes); size_t ready (intFiledes, void * buff, size_t maxlen); -------------- ------------ -------------------------------------------------- -------- Address conversion function #include int inet_aton (const char * strptr, struct in_addr * addr); return: 1 - String effect, 0 - string error in_addr_t inet_addr ( Const char * strptr; return: If successful, return 32-bit binary network word sequence address; if an error, return INADDR_NONE CHAR * INET_NTOA (Struct in_addr inaddr); return: Pointing point decimal number string pointer ine-aton The C string referred to in STRPTR is converted to a 32-bit network word sequence binary value and store it through the pointer ADDRPTR. If successfully returns 1, it will return 0. INET-ADDR converts the C string referred to since StrPtr into a 32-bit network word sequencing binary value and returns through the valence value. The function INET-NTOA converts a 32-bit network word sequencing binary IPv4 address into a corresponding point of decimal string.

转载请注明原文地址:https://www.9cbs.com/read-54987.html

New Post(0)