| United States-English |
|
|
|
![]() |
System Calls and Libraries: Sections 2 and 3 (Ref Pages Vol 3) > cconnect(2) |
|
NAMEconnect — initiate a connection on a socket DESCRIPTIONThe connect() function initiates a connection on a socket. s is a socket descriptor. addr is a pointer to a socket address structure containing the address of a remote socket to which a connection is to be established. addrlen is the size of this address structure. Since the size of the socket address structure varies among socket address families, the correct socket address structure should be used with each address family (for example, struct sockaddr_in for AF_INET and AF_VME_LINK and struct sockaddr_un for AF_UNIX). Typically, the sizeof() function is used to pass this value (for example, sizeof(struct sockaddr_in)). If the socket is of type SOCK_DGRAM, connect() specifies the peer address to which messages are to be sent, and the call returns immediately. Furthermore, this socket can only receive messages sent from this address. If the socket is of type SOCK_STREAM, connect() attempts to contact the remote host to make a connection between the remote socket (peer) and the local socket specified by s. The call normally blocks until the connection completes. If nonblocking mode has been enabled with the O_NONBLOCK or O_NDELAY fcntl() flags or the FIOSNBIO ioctl() request and the connection cannot be completed immediately, connect() returns an error as described below. In these cases, select() can be used on this socket to determine when the connection has completed by selecting it for writing. The connect() system call may complete if remote program has a pending listen() even though remote program had not yet issued an accept() system call. O_NONBLOCK and O_NDELAY are defined in <sys/fcntl.h> and explained in fcntl(2), fcntl(5), and socket(7). FIOSNBIO is defined in <sys/ioctl.h> and explained in ioctl(2), ioctl(5), and socket(7). If s is a SOCK_STREAM socket that is bound to the same local address as another SOCK_STREAM socket, connect() returns [EADDRINUSE] if addr is the same as the peer address of that other socket. This situation can only happen if the SO_REUSEADDR option has been set on s, which is an AF_INET socket (see getsockopt(2)). If the AF_INET socket does not already have a local address bound to it (see bind(2)), connect() also binds the socket to a local address chosen by the system. An AF_VME_LINK socket always binds the socket to a local address chosen by the system. Generally, stream sockets may successfully connect only once; datagram sockets may use connect() multiple times to change the peer address. For datagram sockets, a side effect of attempting to connect to some invalid address (see ERRORS below) is that the peer address is no longer maintained by the system. An example of an invalid address for a datagram socket is addrlen set to 0 and addr set to any value. AF_CCITT OnlyUse the x25addrstr struct for the address structure. The caller must know the X.121 address of the DTE to which the connection is to be established, including any subaddresses or protocol IDs that may be needed. Refer to af_ccitt(7F) for a detailed description of the x25addrstr address structure. If address-matching by protocol ID, specify the protocol ID with the X25_WR_USER_DATA ioctl() call before issuing the connect() call. The X25_WR_USER_DATA ioctl() call is described in socketx25(7). RETURN VALUEconnect() returns the following values:
ERRORSIf connect() fails, errno is set to one of the following values.
FUTURE DIRECTIONThe default behavior in this release is still the classic HP-UX BSD Sockets, however it will be changed to X/Open Sockets in some future release. At that time, any HP-UX BSD Sockets behavior which is incompatible with X/Open Sockets may be obsoleted. HP customers are advised to migrate their applications to conform to X/Open specification (see xopen_networking(7)). MULTITHREAD USAGEThe connect() system call is safe to be called by multithreaded applications, and it is thread-safe for both POSIX Threads and DCE User Threads. It has a cancellation point. It is async-cancel safe, async-signal safe, and fork-safe. SEE ALSOaccept(2), getsockname(2), select(2), socket(2), af_ccitt(7F), af_vme_link(7F), socket(7), socketx25(7), xopen_networking(7). |
||||||||||||||||||||||||||||
|
|||||||||||||||