1CONNECT(2)                 Linux Programmer's Manual                CONNECT(2)
2
3
4

NAME

6       connect - initiate a connection on a socket
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/socket.h>
11
12       int  connect(int  sockfd,  const  struct sockaddr *serv_addr, socklen_t
13       addrlen);
14

DESCRIPTION

16       The connect() system call connects the socket referred to by  the  file
17       descriptor  sockfd  to the address specified by serv_addr.  The addrlen
18       argument specifies the size of serv_addr.  The format of the address in
19       serv_addr  is determined by the address space of the socket sockfd; see
20       socket(2) for further details.
21
22       If the socket sockfd is  of  type  SOCK_DGRAM  then  serv_addr  is  the
23       address  to  which  datagrams are sent by default, and the only address
24       from  which  datagrams  are  received.   If  the  socket  is  of   type
25       SOCK_STREAM  or SOCK_SEQPACKET, this call attempts to make a connection
26       to the socket that is bound to the address specified by serv_addr.
27
28       Generally, connection-based protocol sockets may successfully connect()
29       only  once;  connectionless protocol sockets may use connect() multiple
30       times to change their association.  Connectionless sockets may dissolve
31       the  association  by connecting to an address with the sa_family member
32       of sockaddr set to AF_UNSPEC.
33

RETURN VALUE

35       If the connection or binding succeeds, zero is returned.  On error,  -1
36       is returned, and errno is set appropriately.
37

ERRORS

39       The  following  are  general  socket  errors  only.  There may be other
40       domain-specific error codes.
41
42       EACCES For Unix domain sockets, which are identified by pathname: Write
43              permission is denied on the socket file, or search permission is
44              denied for one of the directories in the path prefix.  (See also
45              path_resolution(2).)
46
47       EACCES, EPERM
48              The  user tried to connect to a broadcast address without having
49              the socket broadcast flag  enabled  or  the  connection  request
50              failed because of a local firewall rule.
51
52       EADDRINUSE
53              Local address is already in use.
54
55       EAFNOSUPPORT
56              The passed address didn't have the correct address family in its
57              sa_family field.
58
59       EAGAIN No more free local ports or insufficient entries in the  routing
60              cache.  For  PF_INET see the net.ipv4.ip_local_port_range sysctl
61              in ip(7) on how to increase the number of local ports.
62
63       EALREADY
64              The socket is non-blocking and a previous connection attempt has
65              not yet been completed.
66
67       EBADF  The  file  descriptor is not a valid index in the descriptor ta‐
68              ble.
69
70       ECONNREFUSED
71              No one listening on the remote address.
72
73       EFAULT The socket structure  address  is  outside  the  user's  address
74              space.
75
76       EINPROGRESS
77              The  socket  is  non-blocking  and the connection cannot be com‐
78              pleted immediately.  It is possible to select(2) or poll(2)  for
79              completion  by selecting the socket for writing. After select(2)
80              indicates writability, use getsockopt(2) to  read  the  SO_ERROR
81              option  at  level SOL_SOCKET to determine whether connect() com‐
82              pleted  successfully  (SO_ERROR  is  zero)   or   unsuccessfully
83              (SO_ERROR  is one of the usual error codes listed here, explain‐
84              ing the reason for the failure).
85
86       EINTR  The system call was interrupted by a signal that was caught.
87
88       EISCONN
89              The socket is already connected.
90
91       ENETUNREACH
92              Network is unreachable.
93
94       ENOTSOCK
95              The file descriptor is not associated with a socket.
96
97       ETIMEDOUT
98              Timeout while attempting connection. The server may be too  busy
99              to  accept new connections. Note that for IP sockets the timeout
100              may be very long when syncookies are enabled on the server.
101

CONFORMING TO

103       SVr4, 4.4BSD,  (the  connect()  function  first  appeared  in  4.2BSD),
104       POSIX.1-2001.
105

NOTE

107       The  third argument of connect() is in reality an int (and this is what
108       4.x BSD and libc4 and libc5 have).  Some POSIX  confusion  resulted  in
109       the present socklen_t, also used by glibc.  See also accept(2).
110

BUGS

112       Unconnecting  a socket by calling connect() with a AF_UNSPEC address is
113       not yet implemented.
114

SEE ALSO

116       accept(2),  bind(2),  getsockname(2),  listen(2),   path_resolution(2),
117       socket(2)
118
119
120
121Linux 2.6.7                       2004-06-23                        CONNECT(2)
Impressum