|
Packit |
7cfc04 |
'\" et
|
|
Packit |
7cfc04 |
.TH CONNECT "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH PROLOG
|
|
Packit |
7cfc04 |
This manual page is part of the POSIX Programmer's Manual.
|
|
Packit |
7cfc04 |
The Linux implementation of this interface may differ (consult
|
|
Packit |
7cfc04 |
the corresponding Linux manual page for details of Linux behavior),
|
|
Packit |
7cfc04 |
or the interface may not be implemented on Linux.
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
connect
|
|
Packit |
7cfc04 |
\(em connect a socket
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.LP
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
#include <sys/socket.h>
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
int connect(int \fIsocket\fP, const struct sockaddr *\fIaddress\fP,
|
|
Packit |
7cfc04 |
socklen_t \fIaddress_len\fP);
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
function shall attempt to make a connection on a connection-mode
|
|
Packit |
7cfc04 |
socket or to set or reset the peer address of a connectionless-mode
|
|
Packit |
7cfc04 |
socket. The function takes the following arguments:
|
|
Packit |
7cfc04 |
.IP "\fIsocket\fR" 12
|
|
Packit |
7cfc04 |
Specifies the file descriptor associated with the socket.
|
|
Packit |
7cfc04 |
.IP "\fIaddress\fR" 12
|
|
Packit |
7cfc04 |
Points to a
|
|
Packit |
7cfc04 |
.BR sockaddr
|
|
Packit |
7cfc04 |
structure containing the peer address. The length and format of the
|
|
Packit |
7cfc04 |
address depend on the address family of the socket.
|
|
Packit |
7cfc04 |
.IP "\fIaddress_len\fR" 12
|
|
Packit |
7cfc04 |
Specifies the length of the
|
|
Packit |
7cfc04 |
.BR sockaddr
|
|
Packit |
7cfc04 |
structure pointed to by the
|
|
Packit |
7cfc04 |
.IR address
|
|
Packit |
7cfc04 |
argument.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
If the socket has not already been bound to a local address,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall bind it to an address which, unless the socket's address family
|
|
Packit |
7cfc04 |
is AF_UNIX, is an unused local address.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
If the initiating socket is not connection-mode, then
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall set the socket's peer address, and no connection is made. For
|
|
Packit |
7cfc04 |
SOCK_DGRAM sockets, the peer address identifies where all datagrams are
|
|
Packit |
7cfc04 |
sent on subsequent
|
|
Packit |
7cfc04 |
\fIsend\fR()
|
|
Packit |
7cfc04 |
functions, and limits the remote sender for subsequent
|
|
Packit |
7cfc04 |
\fIrecv\fR()
|
|
Packit |
7cfc04 |
functions. If the
|
|
Packit |
7cfc04 |
.IR sa_family
|
|
Packit |
7cfc04 |
member of
|
|
Packit |
7cfc04 |
.IR address
|
|
Packit |
7cfc04 |
is AF_UNSPEC, the socket's peer address shall be reset. Note that despite
|
|
Packit |
7cfc04 |
no connection being made, the term ``connected'' is used to describe a
|
|
Packit |
7cfc04 |
connectionless-mode socket for which a peer address has been set.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
If the initiating socket is connection-mode, then
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall attempt to establish a connection to the address specified by the
|
|
Packit |
7cfc04 |
.IR address
|
|
Packit |
7cfc04 |
argument. If the connection cannot be established immediately and
|
|
Packit |
7cfc04 |
O_NONBLOCK is not set for the file descriptor for the socket,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall block for up to an unspecified timeout interval until the
|
|
Packit |
7cfc04 |
connection is established. If the timeout interval expires before the
|
|
Packit |
7cfc04 |
connection is established,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall fail and the connection attempt shall be aborted. If
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
is interrupted by a signal that is caught while blocked waiting to
|
|
Packit |
7cfc04 |
establish a connection,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall fail and set
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
to
|
|
Packit |
7cfc04 |
.BR [EINTR] ,
|
|
Packit |
7cfc04 |
but the connection request shall not be aborted, and the connection
|
|
Packit |
7cfc04 |
shall be established asynchronously.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
If the connection cannot be established immediately and O_NONBLOCK is
|
|
Packit |
7cfc04 |
set for the file descriptor for the socket,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall fail and set
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
to
|
|
Packit |
7cfc04 |
.BR [EINPROGRESS] ,
|
|
Packit |
7cfc04 |
but the connection request shall not be aborted, and the connection
|
|
Packit |
7cfc04 |
shall be established asynchronously. Subsequent calls to
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
for the same socket, before the connection is established, shall fail
|
|
Packit |
7cfc04 |
and set
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
to
|
|
Packit |
7cfc04 |
.BR [EALREADY] .
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
When the connection has been established asynchronously,
|
|
Packit |
7cfc04 |
\fIpselect\fR(),
|
|
Packit |
7cfc04 |
\fIselect\fR(),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
\fIpoll\fR()
|
|
Packit |
7cfc04 |
shall indicate that the file descriptor for the socket is ready for
|
|
Packit |
7cfc04 |
writing.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The socket in use may require the process to have appropriate
|
|
Packit |
7cfc04 |
privileges to use the
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
function.
|
|
Packit |
7cfc04 |
.SH "RETURN VALUE"
|
|
Packit |
7cfc04 |
Upon successful completion,
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall return 0; otherwise, \(mi1 shall be returned and
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
set to indicate the error.
|
|
Packit |
7cfc04 |
.SH ERRORS
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
function shall fail if:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EADDRNOTAVAIL
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The specified address is not available from the local machine.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EAFNOSUPPORT
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The specified address is not a valid address for the address family of
|
|
Packit |
7cfc04 |
the specified socket.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EALREADY
|
|
Packit |
7cfc04 |
A connection request is already in progress for the specified socket.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EBADF
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.IR socket
|
|
Packit |
7cfc04 |
argument is not a valid file descriptor.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ECONNREFUSED
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The target address was not listening for connections or refused the
|
|
Packit |
7cfc04 |
connection request.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EINPROGRESS
|
|
Packit |
7cfc04 |
O_NONBLOCK is set for the file descriptor for the socket and the
|
|
Packit |
7cfc04 |
connection cannot be immediately established; the connection shall be
|
|
Packit |
7cfc04 |
established asynchronously.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EINTR
|
|
Packit |
7cfc04 |
The attempt to establish a connection was interrupted by delivery of a
|
|
Packit |
7cfc04 |
signal that was caught; the connection shall be established
|
|
Packit |
7cfc04 |
asynchronously.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EISCONN
|
|
Packit |
7cfc04 |
The specified socket is connection-mode and is already connected.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENETUNREACH
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
No route to the network is present.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOTSOCK
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.IR socket
|
|
Packit |
7cfc04 |
argument does not refer to a socket.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EPROTOTYPE
|
|
Packit |
7cfc04 |
The specified address has a different type than the socket bound to the
|
|
Packit |
7cfc04 |
specified peer address.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ETIMEDOUT
|
|
Packit |
7cfc04 |
The attempt to connect timed out before a connection was made.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
If the address family of the socket is AF_UNIX, then
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
shall fail if:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EIO
|
|
Packit |
7cfc04 |
An I/O error occurred while reading from or writing to the file system.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ELOOP
|
|
Packit |
7cfc04 |
A loop exists in symbolic links encountered during resolution of the
|
|
Packit |
7cfc04 |
pathname in
|
|
Packit |
7cfc04 |
.IR address .
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENAMETOOLONG
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The length of a component of a pathname is longer than
|
|
Packit |
7cfc04 |
{NAME_MAX}.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOENT
|
|
Packit |
7cfc04 |
A component of the pathname does not name an existing file or the
|
|
Packit |
7cfc04 |
pathname is an empty string.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOTDIR
|
|
Packit |
7cfc04 |
A component of the path prefix of the pathname in
|
|
Packit |
7cfc04 |
.IR address
|
|
Packit |
7cfc04 |
names an existing file that is neither a directory nor a symbolic link
|
|
Packit |
7cfc04 |
to a directory, or the pathname in
|
|
Packit |
7cfc04 |
.IR address
|
|
Packit |
7cfc04 |
contains at least one non-\c
|
|
Packit |
7cfc04 |
<slash>
|
|
Packit |
7cfc04 |
character and ends with one or more trailing
|
|
Packit |
7cfc04 |
<slash>
|
|
Packit |
7cfc04 |
characters and the last pathname component names an existing file that
|
|
Packit |
7cfc04 |
is neither a directory nor a symbolic link to a directory.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
function may fail if:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EACCES
|
|
Packit |
7cfc04 |
Search permission is denied for a component of the path prefix; or
|
|
Packit |
7cfc04 |
write access to the named socket is denied.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EADDRINUSE
|
|
Packit |
7cfc04 |
Attempt to establish a connection that uses addresses that are already
|
|
Packit |
7cfc04 |
in use.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ECONNRESET
|
|
Packit |
7cfc04 |
Remote host reset the connection request.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EHOSTUNREACH
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The destination host cannot be reached (probably because the host is
|
|
Packit |
7cfc04 |
down or a remote router cannot reach it).
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EINVAL
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.IR address_len
|
|
Packit |
7cfc04 |
argument is not a valid length for the address family; or invalid
|
|
Packit |
7cfc04 |
address family in the
|
|
Packit |
7cfc04 |
.BR sockaddr
|
|
Packit |
7cfc04 |
structure.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ELOOP
|
|
Packit |
7cfc04 |
More than
|
|
Packit |
7cfc04 |
{SYMLOOP_MAX}
|
|
Packit |
7cfc04 |
symbolic links were encountered during resolution of the pathname in
|
|
Packit |
7cfc04 |
.IR address .
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENAMETOOLONG
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The length of a pathname exceeds
|
|
Packit |
7cfc04 |
{PATH_MAX},
|
|
Packit |
7cfc04 |
or pathname resolution of a symbolic link produced an intermediate
|
|
Packit |
7cfc04 |
result with a length that exceeds
|
|
Packit |
7cfc04 |
{PATH_MAX}.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENETDOWN
|
|
Packit |
7cfc04 |
The local network interface used to reach the destination is down.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOBUFS
|
|
Packit |
7cfc04 |
No buffer space is available.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EOPNOTSUPP
|
|
Packit |
7cfc04 |
The socket is listening and cannot be connected.
|
|
Packit |
7cfc04 |
.LP
|
|
Packit |
7cfc04 |
.IR "The following sections are informative."
|
|
Packit |
7cfc04 |
.SH EXAMPLES
|
|
Packit |
7cfc04 |
None.
|
|
Packit |
7cfc04 |
.SH "APPLICATION USAGE"
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
\fIconnect\fR()
|
|
Packit |
7cfc04 |
fails, the state of the socket is unspecified. Conforming applications
|
|
Packit |
7cfc04 |
should close the file descriptor and create a new socket before
|
|
Packit |
7cfc04 |
attempting to reconnect.
|
|
Packit |
7cfc04 |
.SH "RATIONALE"
|
|
Packit |
7cfc04 |
None.
|
|
Packit |
7cfc04 |
.SH "FUTURE DIRECTIONS"
|
|
Packit |
7cfc04 |
None.
|
|
Packit |
7cfc04 |
.SH "SEE ALSO"
|
|
Packit |
7cfc04 |
.IR "\fIaccept\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIbind\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIclose\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIgetsockname\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIpoll\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIpselect\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIsend\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIshutdown\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIsocket\fR\^(\|)"
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The Base Definitions volume of POSIX.1\(hy2008,
|
|
Packit |
7cfc04 |
.IR "\fB<sys_socket.h>\fP"
|
|
Packit |
7cfc04 |
.SH COPYRIGHT
|
|
Packit |
7cfc04 |
Portions of this text are reprinted and reproduced in electronic form
|
|
Packit |
7cfc04 |
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
|
|
Packit |
7cfc04 |
-- Portable Operating System Interface (POSIX), The Open Group Base
|
|
Packit |
7cfc04 |
Specifications Issue 7, Copyright (C) 2013 by the Institute of
|
|
Packit |
7cfc04 |
Electrical and Electronics Engineers, Inc and The Open Group.
|
|
Packit |
7cfc04 |
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
|
|
Packit |
7cfc04 |
event of any discrepancy between this version and the original IEEE and
|
|
Packit |
7cfc04 |
The Open Group Standard, the original IEEE and The Open Group Standard
|
|
Packit |
7cfc04 |
is the referee document. The original Standard can be obtained online at
|
|
Packit |
7cfc04 |
http://www.unix.org/online.html .
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
Any typographical or formatting errors that appear
|
|
Packit |
7cfc04 |
in this page are most likely
|
|
Packit |
7cfc04 |
to have been introduced during the conversion of the source files to
|
|
Packit |
7cfc04 |
man page format. To report such errors, see
|
|
Packit |
7cfc04 |
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|