CONNECT
Section: Linux Programmer's Manual (2)
Updated: 3 Oct 1998
Index
Return to Main Contents
NAME
connect, connect_secure - initiate a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
int connect_secure(int sockfd, struct sockaddr *serv_addr, int addrlen, security_id_t peer_sid);
DESCRIPTION
The parameter
sockfd
is a socket.
If the socket is of type
SOCK_DGRAM,
these calls specify the peer with which the socket is to be associated;
the 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,
these calls attempt to make a connection to another socket. The other
socket is specified by
serv_addr,
which is an address in the communications space of the socket. Each
communications space interprets the
serv_addr
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 address with the
sa_family
sockaddr
member set to
AF_UNSPEC.
Connect_secure
may be used to specify the desired SID of the peer socket
as well as its address. If the socket is of type
SOCK_DGRAM,
then datagrams may only be sent to or received from a socket
with the specified SID. If the socket is of type
SOCK_STREAM,
then the connection will fail unless the listening socket has the
specified SID. However, the server socket created by the connection
may have a different SID than the listening socket if
listen_secure(2)
was used.
RETURN VALUE
If the connection or binding succeeds, zero is returned. On error, -1 is
returned, and
errno
is set appropriately.
ERRORS
The following are general socket errors only. There may be other
domain-specific error codes.
- EBADF
-
Bad descriptor.
- EFAULT
-
The socket structure address is outside the user's address space.
- ENOTSOCK
-
The descriptor is not associated with a socket.
- EISCONN
-
The socket is already connected.
- ECONNREFUSED
-
Connection refused at server.
- ETIMEDOUT
-
Timeout while attempting connection.
- ENETUNREACH
-
Network is unreachable.
- EADDRINUSE
-
Address is already in use.
- EINPROGRESS
-
The socket is non-blocking and the connection cannot be completed
immediately. It is possible to
select(2)
or
poll(2)
for completion by selecting the socket for writing. After
select
indicates writability, use
getsockopt(2)
to read the
SO_ERROR
option at level
SOL_SOCKET
to determine whether
connect
completed successfully
(SO_ERROR
is zero) or unsuccessfully
(SO_ERROR
is one of the usual error codes listed
above, explaining the reason for the failure).
- EALREADY
-
The socket is non-blocking and a previous connection attempt has not yet
been completed.
- EAFNOSUPPORT
-
The passed address didn't have the correct address family in its
sa_family
field.
- EACCES
-
The user tried to connect to a broadcast address without having the socket
broadcast flag enabled.
CONFORMING TO
SVr4, 4.4BSD (the
connect
function first appeared in BSD 4.2). SVr4 documents the additional
general error codes
EADDRNOTAVAIL,
EINVAL,
EAFNOSUPPORT,
EALREADY,
EINTR,
EPROTOTYPE,
and
ENOSR.
It also
documents many additional error conditions not described here.
SEE ALSO
accept(2), bind(2), listen(2),
socket(2), getsockname(2)
Index
- NAME
-
- SYNOPSIS
-
- DESCRIPTION
-
- RETURN VALUE
-
- ERRORS
-
- CONFORMING TO
-
- SEE ALSO
-
This document was created by
man2html,
using the manual pages.
Time: 17:08:26 GMT, December 18, 2000