Windows 和 Linux 上常用的 socket API 函数并不多,除了特定操作系统提供的一些基于自身系统特性的 API, 大多数 Socket API 都源于BSD Socket (即伯克利套接字(Berkeley Sockets)),因此这些 socket 函数在不同的平台有着相似的签名和参数。
[root@localhost ~]# man connect CONNECT(2) Linux Programmer's Manual CONNECT(2)
NAME connect - initiate a connection on a socket
SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
DESCRIPTION The connect() system call connects the socket referred tobythefile descriptor sockfd tothe address specified by addr. The addrlen argument specifies the size of addr. The format ofthe address in addr is determined bythe address spaceofthe socket sockfd; see socket(2) for further details.
If the socket sockfd isof type SOCK_DGRAM then addr isthe address to which datagrams are sent by default, andthe only address from which datagrams are received. If the socket isof type SOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection tothe socket thatis bound tothe address specified by addr.
Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple timesto change their association. Connectionless sockets may dissolve the association by connecting to an address withthe sa_family member of sockaddr setto AF_UNSPEC (supported on Linux since kernel 2.2).
RETURN VALUE If the connection or binding succeeds, zero is returned. On error, -1is returned, and errno isset appropriately.
ERRORS The following are general socket errors only. There may be other domain-specific error codes.
EACCES For UNIX domain sockets, which are identified by pathname: Write permission is denied onthe socket file, or search permission is denied for one ofthe directories inthe path prefix. (See also path_resolution(7).)
EACCES, EPERM The user tried to connect to a broadcast address without having the socket broadcast flag enabled orthe connection request failed because of a local firewall rule.
[root@localhost ~]# manman ## 无关的部分,省略... The table below shows the section numbers of the manual followed by the types of pages they contain.
1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard]
[root@localhost ~]# man 3 sleep SLEEP(3) Linux Programmer's Manual SLEEP(3)
NAME sleep - sleep for the specified number of seconds
SYNOPSIS #include <unistd.h>
unsigned int sleep(unsigned int seconds);
DESCRIPTION sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which isnot ignored.
RETURN VALUE Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.
CONFORMING TO POSIX.1-2001.
BUGS sleep() may be implemented using SIGALRM; mixing calls to alarm(2) and sleep() is a bad idea.
Using longjmp(3) from a signal handler or modifying the handling of SIGALRM while sleeping will cause undefined results.
SEE ALSO alarm(2), nanosleep(2), signal(2), signal(7)
COLOPHON This page is part ofrelease3.53of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.
Windows 上查看 socket 函数帮助
Windows 也有类似 man 手册的帮助文档,早些年 Visual Studio 会自带一套离线的 MSDN 文档库,其优点就是不需要电脑联网,缺点是占磁盘空间比较大,内容陈旧。在手机网络都如此普及的今天,笔者还是建议使用在线版本的 MSDN。查看 Windows API 的帮助链接是:https://docs.microsoft.com/en-us/windows/desktop/ ,在页面的搜索框中输入你想要搜索的 API 函数即可。
需要注意的是,在 MSDN 上阅读相关 API 的帮助信息时,你要辩证性地对待其提供的信息,因为很多函数的实际工作原理和行为并不一定如 MSDN 介绍的那样。所以在有些 API 帮助下面会有一些读者的评论信息,这些评论信息或对文档内容做一些补充或纠错,或给出一些代码示例。建议读者实际查阅时,留意一下这部分信息,或许能得到一些很有用的帮助。