WSADATA wsaData; WSAStartup( MAKEWORD(2, 2), &wsaData);
4. socket编程算法
int shutdown(int sock, int howto); //Linux int shutdown(SOCKET s, int howto); //Windows
sock 为须要断开的套接字,howto 为断开方式编程
howto 在 Linux 下有如下取值:#include <netdb.h> struct hostent *gethostbyname(const char *hostname);
成功时返回hostnet结构体指针,失败时返回NULL指针服务器
struct hostnet { char *h_name; char **h_aliases; int h_addrtype; int h_lenght; char **h_addr_list; }
(2) gethostbyaddr()函数利用IP地址获取域相关信息网络
#include <netdb.h> struct hostnet *gethostbyaddr(const char *addr, socklen_t len, int fanily);
成功时返回hostnet结构体变量地址值,失败时返回NULL指针socket
addr:含有IP地址信息的in_addr结构体指针unsigned short htons(unsigned short); unsigned short ntohs(unsigned short); unsigned long htonl(unsigned long); unsigned long ntohl(unsigned long);
(4) 将字符串信息转换为网络字节序的整数型函数
#include <arpa/inet.h> in_addr_t inet_addr(const char *string);
成功时返回32位大端序整数型值,失败时返回INADDR_NONEspa
(5) inet_aton()也将字符串形式IP地址转换位32位网络字节序整数并返回,只不过该函数利用in_addr结构体,且其使用频率跟高#include <arpa/inet.h> int inet_aton(const char *string, struct in_addr *addr);
成功时返回1(true),失败时返回0(false)指针
(6) inet_ntoa()函数能够把网络字节序整数型IP地址转换成字符串形式#include <arpa/inet.h> char *inet_ntoa(struct in_addr adr);
成功时返回转换的字符串地址值,失败时返回-1;code
调用完该函数后应当即将字符串信息复制到其余内存空间,由于,若再次调用inet_ntoa函数,则有可能覆盖以前保存的字符串信息struct sockaddr_in addr; char *serv_ip = "211.217.168.13"; char *serv_port "9190"; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(serv_ip); addr.sin_port = htons(atoi(serv_port));
利用常数INADDR_ANY分配服务器的IP地址,能够自动获取运行服务器端的计算机IP地址对象
addr.sin_addr.s_addr = htonl(INADDR_ANY);getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen); setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
#include <unistd.h> pid_t fork(void);
父进程:fork函数返回子进程的ID
子进程:fork函数返回0#include <unistd.h> pid_t wait(int *statloc);
->成功时返回终止的子进程ID,失败时返回-1
子进程终止时传递的返回值将保存到statloc所指内存空间,须要用下列宏进行分离#include <sys/wait.h> pid_t waitpid(pid_t pid, int *statloc, int options);
->成功时返回终止的子进程ID,失败时返回-1
pid 等待终止的目标子进程ID,若传递-1,则能够等待任意子进程终止#include <signal.h> void (*signal(int signal, void (*func)(void)))(int);
->为了在产生信号时调用,返回以前注册的函数指针
(6) 利用sigaction函数进行信号处理#include <signal.h> int sugacyion(int signo, const struct sigaction *act, struct sigaction *oldact);
->成功时返回0,失败时返回-1
经过fork函数复制套接字文件描述符后,同一端口将对应多个套接字,只有这些套接字描述符都终止,才能销毁套接字#include <unistd.h> int pipe(int filedes[2]);
->成功时返回0,失败时返回-1
filedes[0]:经过管道接收数据时使用的文件描述符,即管道出口FD_ZERO(fd_set *fdset) FD_SET(int fd, fd_set *fdset) FD_CLR(int fd, fd_set *fdset) FD_ISSET(fint fd, d_set *fdset)
(2) select函数:
#include <sys/select.h> #include <sys/time.h> int select(int maxfd, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout);
15.多种I/O函数
(1) 收到MSG_OOB紧急消息时,操着系统将产生SIGURG消息,并调用注册的信号处理函数fcntl(recv_sock, F_SETOWN, getpid());
上述调用的含义是“将文件描述符recv_sock指向的套接字拥有者(F_SETOWN)改成把getpid函数返回值用做ID的进程
(4) 紧急指针指向紧急消息的下一个位置(偏移量+1),紧急消息的意义在于督促消息处理,而非紧急传输形式受限的消息 (5) 调用recv函数的同时传递MSG_PEEK可选项,是为了保证即便不存在待读取的数据也不会进入阻塞状态,设置MSG_PEEK选项并调用recv函数时,即便读取了输入缓冲的数据也不会删除,该选项一般与MSG_DONTWAIT合做,用于调用以非阻塞方式验证待读取数据存在与否的函数