Unix 套接字 - 摘要

以下是与套接字编程相关的所有函数的列表。

端口和服务函数

Unix 提供以下函数从 /etc/services 文件中获取服务名称。

  • struct servent *getservbyname(char *name, char *proto) − 此调用接受服务名称和协议名称,并返回该服务的相应端口号。

  • struct servent *getservbyport(int port, char *proto) −此调用接受端口号和协议名称并返回相应的服务名称。

字节排序函数

  • unsigned short htons (unsigned short hostshort) − 此函数将 16 位(2 字节)数量从主机字节顺序转换为网络字节顺序。

  • unsigned long htonl (unsigned long hostlong) − 此函数将 32 位(4 字节)数量从主机字节顺序转换为网络字节顺序。

  • unsigned short ntohs (unsigned short netshort) −此函数将 16 位(2 字节)数量从网络字节顺序转换为主机字节顺序。

  • unsigned long ntohl (unsigned long netlong) − 此函数将 32 位数量从网络字节顺序转换为主机字节顺序。

IP 地址函数

  • int inet_aton (const char *strptr, struct in_addr *addrptr) − 此函数调用将指定的字符串(以 Internet 标准点表示法)转换为网络地址,并将该地址存储在提供的结构中。转换后的地址将采用网络字节顺序(字节从左到右排序)。如果字符串有效,则返回 1,如果出错,则返回 0。

  • in_addr_t inet_addr (const char *strptr) − 此函数调用将指定的字符串(以 Internet 标准点表示法)转换为适合用作 Internet 地址的整数值。转换后的地址将采用网络字节顺序(字节从左到右排序)。它返回 32 位二进制网络字节排序的 IPv4 地址,如果出错,则返回 INADDR_NONE。

  • char *inet_ntoa (struct in_addr inaddr) − 此函数调用将指定的 Internet 主机地址转换为以 Internet 标准点表示法表示的字符串。

套接字核心函数

  • int socket (int family, int type, int protocol) − 此调用返回一个套接字描述符,您可以在以后的系统调用中使用它,或者在出错时返回 -1。

  • int connect (int sockfd, struct sockaddr *serv_addr, int addrlen) − TCP 客户端使用 connect 函数与 TCP 服务器建立连接。如果成功连接到服务器,此调用将返回 0,否则返回 -1。

  • int bind(int sockfd, struct sockaddr *my_addr,int addrlen) − bind 函数将本地协议地址分配给套接字。如果成功绑定到地址,此调用将返回 0,否则返回 -1。

  • int listen(int sockfd, int backlog) − 仅 TCP 服务器调用 listen 函数来侦听客户端请求。成功时,此调用返回 0,否则返回 -1。

  • int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) − TCP 服务器调用 accept 函数来接受客户端请求并建立实际连接。成功时,此调用返回非负描述符,否则返回 -1。

  • int send(int sockfd, const void *msg, int len, int flags) − send 函数用于通过流套接字或 CONNECTED 数据报套接字发送数据。此调用返回发送的字节数,否则返回 -1。

  • int recv (int sockfd, void *buf, int len, unsigned int flags) − recv 函数用于通过流套接字或 CONNECTED 数据报套接字接收数据。此调用返回读入缓冲区的字节数,否则在出错时返回 -1。

  • int sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen) − sendto 函数用于通过 UNCONNECTED 数据报套接字发送数据。此调用返回发送的字节数,否则出错时返回 -1。

  • int recvfrom (int sockfd, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen) − recvfrom 函数用于从未连接的数据报套接字接收数据。此调用返回读入缓冲区的字节数,否则出错时返回 -1。

  • int close (int sockfd) − close 函数用于关闭客户端与服务器之间的通信。此调用成功时返回 0,否则返回 -1。

  • int shutdown (int sockfd, int how) − shutdown 函数用于正常关闭客户端与服务器之间的通信。与 close 函数相比,此函数提供更多控制。成功时返回 0,否则返回 -1。

  • int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) − 此函数用于读取或写入多个套接字。

套接字辅助函数

  • int write (int fildes, const void *buf, int nbyte) − write 函数尝试将 buf 指向的缓冲区中的 nbyte 字节写入与打开的文件描述符 fildes 关联的文件。成功完成后,write() 将返回实际写入与 fildes 关联的文件的字节数。此数字永远不会大于 nbyte。否则,返回 -1。

  • int read (int fildes, const void *buf, int nbyte) − read 函数尝试从与打开的文件描述符 fildes 关联的文件中读取 nbyte 字节,并将其放入 buf 指向的缓冲区中。成功完成后,write() 将返回实际写入与 fildes 关联的文件的字节数。此数字永远不会大于 nbyte。否则,返回 -1。

  • int fork (void) − fork 函数创建一个新进程。新进程称为子进程,它将是调用进程(父进程)的精确副本。

  • void bzero (void *s, int nbyte) − bzero 函数将 nbyte 空字节放入字符串 s 中。此函数将用于将所有套接字结构设置为空值。

  • int bcmp (const void *s1, const void *s2, int nbyte) − bcmp 函数将字节字符串 s1 与字节字符串 s2 进行比较。假设两个字符串的长度均为 nbyte 字节。

  • void bcopy (const void *s1, void *s2, int nbyte) − bcopy 函数将 nbyte 字节从字符串 s1 复制到字符串 s2。重叠字符串可正确处理。

  • void *memset(void *s, int c, int nbyte) − memset 函数也用于以与 bzero 相同的方式设置结构变量。