CONNECT(3XNET) | X/Open Networking Services Library Functions | CONNECT(3XNET) |
connect - connect a socket
cc [ flag ... ] file ... -lxnet [ library ... ] #include <sys/socket.h> int connect(int socket, const struct sockaddr *address,
socklen_t address_len);
The connect() function requests a connection to be made on a socket. The function takes the following arguments:
socket
address
address_len
If the socket has not already been bound to a local address, connect() will bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.
If the initiating socket is not connection-mode, then connect() sets the socket's peer address, but no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send(3XNET) calls, and limits the remote sender for subsequent recv(3XNET) calls. If address is a null address for the protocol, the socket's peer address will be reset.
If the initiating socket is connection-mode, then connect() attempts to establish a connection to the address specified by the address argument.
If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect() will block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect() will fail and the connection attempt will be aborted. If connect() is interrupted by a signal that is caught while blocked waiting to establish a connection, connect() will fail and set errno to EINTR, but the connection request will not be aborted, and the connection will be established asynchronously.
If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() will fail and set errno to EINPROGRESS, but the connection request will not be aborted, and the connection will be established asynchronously. Subsequent calls to connect() for the same socket, before the connection is established, will fail and set errno to EALREADY.
When the connection has been established asynchronously, select(3C) and poll(2) will indicate that the file descriptor for the socket is ready for writing.
The socket in use may require the process to have appropriate privileges to use the connect() function.
If connect() fails, the state of the socket is unspecified. Portable applications should close the file descriptor and create a new socket before attempting to reconnect.
Upon successful completion, connect() returns 0. Otherwise, −1 is returned and errno is set to indicate the error.
The connect() function will fail if:
EADDRNOTAVAIL
EAFNOSUPPORT
EALREADY
EBADF
ECONNREFUSED
EFAULT
EINPROGRESS
EINTR
EISCONN
ENETUNREACH
ENOTSOCK
EPROTOTYPE
ETIMEDOUT
If the address family of the socket is AF_UNIX, then connect() will fail if:
EIO
ELOOP
ENAMETOOLONG
ENOENT
ENOTDIR
The connect() function may fail if:
EACCES
EADDRINUSE
ECONNRESET
EHOSTUNREACH
EINVAL
ENAMETOOLONG
ENETDOWN
ENOBUFS
ENOSR
EOPNOTSUPP
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Standard |
MT-Level | MT-Safe |
close(2), poll(2), select(3C), sockaddr(3SOCKET), accept(3XNET), bind(3XNET), getsockname(3XNET), send(3XNET), shutdown(3XNET), socket(3XNET), attributes(7), standards(7)
June 10, 2002 | OmniOS |