CONNECT(3P)
π¦ CONNECT(3P) β POSIX Programmer's Manual π¦
okay bestie, welcome to the connect() page, aka the moment your socket finally slides into someone's DMs. it lives in the POSIX Programmer's Manual, volume 3P. β¨
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for details on Linux behavior, fr), or honestly the interface might not even be implemented on Linux at all. π
π NAME π
connect β connect a socket
β¨ SYNOPSIS β¨
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address,
socklen_t address_len);
π DESCRIPTION π
so what does connect() actually do? it shall attempt to make a connection on a connection-mode socket, or to set or reset the peer address of a connectionless-mode socket. she's versatile like that. π«Ά
the function takes the following arguments:
socketβ specifies the file descriptor associated with the socket. that's your lil handle bestie.addressβ points to asockaddrstructure containing the peer address. the length and format of the address depend on the address family of the socket. so like, it's giving "depends on the vibe of your family".address_lenβ specifies the length of thesockaddrstructure pointed to by the address argument.
if the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address. yes she'll pick a lil address for you. so thoughtful. π
if the initiating socket is not connection-mode, then connect() shall set the socket's peer address, and no connection is made. for SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions. and if the sa_family member of address is AF_UNSPEC, the socket's peer address shall be reset. also, note that despite no connection being made, the term ``connected'' is used to describe a connectionless-mode socket for which a peer address has been set. yes girl, the terminology is a lil chaotic, we don't make the rules. π
if the initiating socket is connection-mode, then connect() shall attempt to establish a connection to the address specified by the address argument. here's the tea:
- if the connection cannot be established immediately and
O_NONBLOCKis not set for the file descriptor for the socket,connect()shall block for up to an unspecified timeout interval until the connection is established. if the timeout interval expires before the connection is established,connect()shall fail and the connection attempt shall be aborted. π - if
connect()is interrupted by a signal that is caught while blocked waiting to establish a connection,connect()shall fail and seterrnoto[EINTR], but the connection request shall not be aborted, and the connection shall be established asynchronously. iconic behavior honestly. - if the connection cannot be established immediately and
O_NONBLOCKis set for the file descriptor for the socket,connect()shall fail and seterrnoto[EINPROGRESS], but again the connection request shall not be aborted, and the connection shall be established asynchronously. subsequent calls toconnect()for the same socket, before the connection is established, shall fail and seterrnoto[EALREADY]. girl, be patient. π§
when the connection has been established asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for writing. periodt.
β οΈ also the socket in use may require the process to have appropriate privileges to use the connect() function. so like, permissions check, bestie.
π RETURN VALUE π
upon successful completion, connect() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error. classic.
π ERRORS π
the connect() function shall fail if:
EADDRNOTAVAILβ the specified address is not available from the local machine.EAFNOSUPPORTβ the specified address is not a valid address for the address family of the specified socket.EALREADYβ a connection request is already in progress for the specified socket.EBADFβ the socket argument is not a valid file descriptor.ECONNREFUSEDβ the target address was not listening for connections or refused the connection request. ouch. rejected. πEINPROGRESSβO_NONBLOCKis set for the file descriptor for the socket and the connection cannot be immediately established; the connection shall be established asynchronously.EINTRβ the attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection shall be established asynchronously.EISCONNβ the specified socket is connection-mode and is already connected.ENETUNREACHβ no route to the network is present.ENOTSOCKβ the socket argument does not refer to a socket.EPROTOTYPEβ the specified address has a different type than the socket bound to the specified peer address.ETIMEDOUTβ the attempt to connect timed out before a connection was made.
if the address family of the socket is AF_UNIX, then connect() shall fail if:
EIOβ an I/O error occurred while reading from or writing to the file system.ELOOPβ a loop exists in symbolic links encountered during resolution of the pathname in address.ENAMETOOLONGβ the length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ a component of the pathname does not name an existing file or the pathname is an empty string.ENOTDIRβ a component of the path prefix of the pathname in address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in address contains at least one non-<slash>character and ends with one or more trailing<slash>characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory. (yes that's a lot of words, it's fine, it's fine.)
the connect() function may fail if:
EACCESβ search permission is denied for a component of the path prefix; or write access to the named socket is denied.EADDRINUSEβ attempt to establish a connection that uses addresses that are already in use.ECONNRESETβ remote host reset the connection request.EHOSTUNREACHβ the destination host cannot be reached (probably because the host is down or a remote router cannot reach it).EINVALβ the address_len argument is not a valid length for the address family; or invalid address family in thesockaddrstructure.ELOOPβ more than{SYMLOOP_MAX}symbolic links were encountered during resolution of the pathname in address.ENAMETOOLONGβ the length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}.ENETDOWNβ the local network interface used to reach the destination is down.ENOBUFSβ no buffer space is available.EOPNOTSUPPβ the socket is listening and cannot be connected.
π the following sections are informative. (translation: they're the bonus content, not the rules.)
π EXAMPLES π
None.
like. none. zero. the void. nothing here bestie. π
π APPLICATION USAGE π
if connect() fails, the state of the socket is unspecified. so conforming applications should close() the file descriptor and create a new socket before attempting to reconnect. no reusing that mess, start fresh, it's giving rebrand. β¨
π§ RATIONALE π§
None.
π¦ FUTURE DIRECTIONS π¦
None.
π SEE ALSO π
accept(), bind(), close(), getsockname(), poll(), pselect(), send(), shutdown(), socket()
the Base Definitions volume of POSIX.1β2017, <sys_socket.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's the whole page bestie!! this one was brought to you by IEEE/The Open Group, 2017 β so like, cite her properly, she's a queen. π now go connect to something. responsibly. π π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms