connect.2 (2010 09)

c
connect(2) connect(2)
NAME
connect - initiate a connection on a socket
SYNOPSIS
#include <sys/socket.h>
AF_CCITT only
#include <x25/x25addrstr.h>
AF_INET, AF_INET6 and AF_VME_LINK only
#include <netinet/in.h>
AF_UNIX only
#include <sys/un.h>
int connect(int s, const void *addr, int addrlen);
UNIX 03 only (X/Open Sockets)
int connect(int s, const struct sockaddr *addr, socklen_t addrlen);
Obsolescent UNIX 95 only (X/Open Sockets)
int connect(int s, const struct sockaddr *addr, size_t addrlen);
DESCRIPTION
The 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 con-
nection 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, struct sockaddr_in6 for
AF_INET6, 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 con-
nection 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 or
AF_INET6 socket (see getsockopt (2)).
If the AF_INET or AF_INET6 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 sys-
tem. An example of an invalid address for a datagram socket is addrlen set to 0 and addr set to any
value.
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (4 pages)