CONNECT(2) CONNECT(2) NAME connect - initiate a connection on a socket SYNOPSIS #include <sys/types.h> #include <sys/socket.h> int connect (int s, const struct sockaddr *name, socklen_t namelen); #if _XOPEN_SOURCE >= 500 int connect (int s, const struct sockaddr *name, socklen_t namelen); #elif _XOPEN_SOURCE < 500 int connect (int s, const struct sockaddr *name, size_t namelen); #endif DESCRIPTION The parameter s is a socket. If it is of type SOCK_DGRAM, then this call specifies the peer with which the socket is to be associated; this address is that to which datagrams are to be sent, and the only address from which datagrams are to be received. If the socket is of type SOCK_STREAM, then this call attempts to make a connection to another socket. The other socket is specified by name, which is an address in the communications space of the socket. Each communications space interprets the name parameter in its own way. Generally, stream sockets may successfully connect only once; datagram sockets may use connect multiple times to change their association. Datagram sockets may dissolve the association by connecting to an invalid address, such as a zero-filled address. RETURN VALUE If the connection or binding succeeds, then 0 is returned. Otherwise a -1 is returned, and a more specific error code is stored in errno. ERRORS The call fails if: [EBADF] S is not a valid descriptor. [ENOTSOCK] S is a descriptor for a file, not a socket. [EADDRNOTAVAIL] The specified address is not available on this machine. [EAFNOSUPPORT] Addresses in the specified address family cannot be used with this socket. [EISCONN] The socket is already connected. [ETIMEDOUT] Connection establishment timed out without establishing a connection. [ECONNREFUSED] The attempt to connect was forcefully rejected. [ENETUNREACH] The network isn't reachable from this host. [EADDRINUSE] The address is already in use. [EFAULT] The name parameter specifies an area outside the process address space. [EINPROGRESS] The socket is non-blocking and the connection cannot be completed immediately. It is possible to select(2) for completion by selecting the socket for writing. [EALREADY] The socket is non-blocking and a previous connection attempt has not yet been completed. See also the protocol-specific manual pages for other error values. SEE ALSO accept(2), select(2), socket(2), tcp(7P), udp(7P), unix(7F) NOTES ABI-compliant versions of the above call can be obtained from libsocket.so. There are three types of connect functions in n32 and 64 bit C libraries for IRIX 6.5.19 and later versions. One is the normal type when _XOPEN_SOURCE is not defined; the second is XPG5 type when _XOPEN_SOURCE is set to >= 500; and the third is XPG4 type when _XOPEN_SOURCE set to < 500. The difference between these functions is in the third argument type to connect. Refer <sys/socket.h> for alternate definitions of socklen_t type. 1. For the normal case when _XOPEN_SOURCE is not defined, third argument type, socklen_t, will be an int and the normal connect is used. 2. When _XOPEN_SOURCE is set to >= 500, third argument type, socklen_t, will be u_int32_t type and xpg5 type function will be used. 3. When _XOPEN_SOURCE is set to < 500, third argument type will be a size_t and xpg4 type function will be used. XPG5 type function is not supported in o32 C library. The XPG5 type connect function is actually defined as a static inline function in <sys/socket.h>, and it calls a new function _xpg5_connect which is specific to IRIX 6.5.19 and later. Therefore applications that call XPG5 type connect should check the existence of the new symbol. #include <sys/socket.h> #include <optional_sym.h> if (_MIPS_SYMBOL_PRESENT(_xpg5_connect)) { connect(s, &name, namelen); } else { ... } Because the static inline function is defined in each source file that includes <sys/socket.h>, these static functions will have different addresses in cases where inline expansion is not performed. This may cause problems if the address of the function is examined in programs. To avoid this problem, use -D_XPG5_CONNECT_USER_DEFINED compile option to disable the static inline definition in <sys/socket.h>, and define a user defined function with below definition: int * connect(int _s, const struct sockaddr *_name, socklen_t _namelen) { return(_xpg5_connect(_s, _name, _namelen)); } Use the compile option always, when a user defined XPG5 connect function is required. Page 3