1. IPv4套接口地址结构
#include <netinet/in.h>
struct in_addr
{
in_addr_t s_addr; /* 32-bit IPv4 address */
/* network byte ordered */
};
struct sockaddr_in
{
uint8_t sin_len; /* length of structure (16) */
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* 16-bit TCP or UDP port number */
/* network byte ordered */
struct in_addr sin_addr; /* 32-bit IPv4 address */
/* network byte ordered */
char sin_zero[8]; /* unused */
};
struct sockaddr
{
uint8_t sa_len;
sa_family_t sa_family; /* address family: AF_xxx value */
char sa_data[14]; /* protocol-specific address */
};
2. IPv6套接口地址结构
#include <netinet/in.h>
struct in6_addr
{
uint8_t s6_addr[16]; /* 128-bit IPv6 address */
/* network byte ordered */
};
#define SIN6_LEN /* required for compile-time tests */
struct sockaddr_in6
{
uint8_t sin6_len; /* length of this struct (28) */
sa_family_t sin6_family; /* AF_INET6 */
in_port_t sin6_port; /* transport layer port# */
/* network byte ordered */
uint32_t sin6_flowinfo; /* flow information, undefined */
struct in6_addr sin6_addr; /* IPv6 address */
/* network byte ordered */
uint32_t sin6_scope_id; /* set of interfaces for a scope */
};
3. 新的通用套接口地址结构
#include <netinet/in.h>
struct sockaddr_storage
{
uint8_t ss_len; /* length of this struct (implementation dependent) */
sa_family_t ss_family; /* address family: AF_xxx value */
/* implementation-dependent elements to provide:
* a) alignment sufficient to fulfill the alignment requirements of
* all socket address types that the system supports.
* b) enough storage to hold any type of socket address that the
* system supports.
*/
};
4.字节排序函数
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue) ;
uint32_t htonl(uint32_t host32bitvalue) ;
Both return: value in network byte order
uint16_t ntohs(uint16_t net16bitvalue) ;
uint32_t ntohl(uint32_t net32bitvalue) ;
Both return: value in host byte order
5.字节操纵函数
#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);
/*Returns: 0 if equal, nonzero if unequal*/
6.IPv4地址转换函数
#include <arpa/inet.h>
/*Returns: 1 if string was valid, 0 on error*/
int inet_aton(const char *strptr, struct in_addr *addrptr);
/*Returns: 32-bit binary network byte ordered IPv4 address; INADDR_NONE if error*/
in_addr_t inet_addr(const char *strptr);
/*Returns: pointer to dotted-decimal string*/
char *inet_ntoa(struct in_addr inaddr);
7.IPv4/IPv6通用地址转换函数
#include <arpa/inet.h>
/*Returns: 1 if OK, 0 if input not a valid presentation format, -1 on error*/
int inet_pton(int family, const char *strptr, void *addrptr);
/*Returns: pointer to result if OK, NULL on error*/
const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);
8.套接口函数
#include <sys/socket.h>
/*Returns: non-negative descriptor if OK, -1 on error*/
int socket(int family, int type, int protocol);
/*Returns: 0 if OK, -1 on error*/
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
/*Returns: 0 if OK,-1 on error*/
int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
/*Returns: 0 if OK, -1 on error*/
#int listen(int sockfd, int backlog);
/*Returns: non-negative descriptor if OK, -1 on error*/
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
9.获得与套接口关联的协议地址
#include <sys/socket.h>
/*Both return: 0 if OK, -1 on error*/
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);
10.I/O复用函数
#include <sys/select.h>
#include <sys/time.h>
/*Returns: positive count of ready descriptors, 0 on timeout, –1 on error*/
int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout);
struct timeval
{
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
void FD_ZERO(fd_set *fdset);
/* clear all bits in fdset */
void FD_SET(int fd, fd_set *fdset);
/* turn on the bit for fd in fdset */
void FD_CLR(int fd, fd_set *fdset);
/* turn off the bit for fd in fdset */
int FD_ISSET(int fd, fd_set *fdset);
/* is the bit for fd on in fdset ? */
#include <sys/select.h>
#include <signal.h>
#include <time.h>
/*Returns: count of ready descriptors, 0 on timeout, –1 on error*/
int pselect (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timespec *timeout, const sigset_t *sigmask);
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
#include <poll.h>
/*Returns: count of ready descriptors, 0 on timeout, –1 on error*/
int poll (struct pollfd *fdarray, unsigned long nfds, int timeout);
struct pollfd
{
int fd; /* descriptor to check */
short events; /* events of interest on fd */
short revents; /* events that occurred on fd */
};
posted on 2009-02-27 09:39
nichii 阅读(151)
评论(0) 编辑 收藏 引用