The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlen argument specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; see socket(2) for further details.
// sockfd // 未连接套接字 // addr // 要连接的地址信息 // addrlen // 地址信息的数据长度 // // return: // If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropri‐ately. // 成功返回0,失败返回-1且会设置errno的值 // // error: // EACCES // For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket file, or search permission is denied for one of the directories in the path prefix. (See also path_resolu‐tion(7).) // EACCES, EPERM // The user tried to connect to a broadcast address without having the socket broadcast flag enabled or the connection request failed because of a local firewall rule. // EADDRINUSE // Local address is already in use. // 这个地址已经在连接 // EAFNOSUPPORT // The passed address didn't have the correct address family in its sa_family field. // sockaddr结构的sa_family不正确。 // EADDRNOTAVAIL // Non-existent interface was requested or the requested address was not local. // EALREADY // The socket is nonblocking and a previous connection attempt has not yet been completed. // socket为不可阻断且先前的连线操作还未完成。 // EBADF // The file descriptor is not a valid index in the descriptor table. // 参数sockfd 非合法socket处理代码 // ECONNREFUSED // No-one listening on the remote address. // 连线要求被server端拒绝。 // EFAULT // The socket structure address is outside the user's address space. // 参数serv_addr指针指向无法存取的内存空间 // EINPROGRESS // The socket is nonblocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed suc‐cessfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure). // EINTR // The system call was interrupted by a signal that was caught; see signal(7). // EISCONN // The socket is already connected. // 参数sockfd的socket已是连线状态 // ENETUNREACH // Network is unreachable. // 无法传送数据包至指定的主机。 // ENOTSOCK // The file descriptor is not associated with a socket. // 参数sockfd为一文件描述词,非socket。 // ETIMEDOUT // Timeout while attempting connection. The server may be too busy to accept new connections. Note that for IP sockets the timeout may be very long when syncookies are enabled on the server. // 企图连线的操作超过限定时间仍未有响应。 // // intconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);