CONNECT(3POSIX)
π CONNECT(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual page for connect() β yes it's that connect(), the socket one, the networking one, the one that makes your little client actually talk to a server. πΈ
π¨ PROLOG π¨
so like, heads up before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so if you're on Linux, go consult the corresponding Linux manual page for the deets on Linux behavior. orrrr the interface might not even be implemented on Linux at all. π no cap. check first.
β¨ NAME β¨
connect β connect a socket π¦
π SYNOPSIS π
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address,
socklen_t address_len);
π DESCRIPTION π
so the connect() function shall attempt to make a connection on a connection-mode socket, or to set or reset the peer address of a connectionless-mode socket. it's giving versatility, honestly. π
the function takes the following arguments:
socketβ specifies the file descriptor associated with the socket. that's your lil handle to the socket, 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 family", literally.address_lenβ specifies the length of thesockaddrstructure pointed to by theaddressargument.
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. β¨
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. if the sa_family member of address is AF_UNSPEC, the socket's peer address shall be reset. and note β despite no connection being made, the term ``connected'' is used to describe a connectionless-mode socket for which a peer address has been set. okay so the vocabulary is just messy like that, we move. π
if the initiating socket is connection-mode, then connect() shall attempt to establish a connection to the address specified by the address argument. if the connection cannot be established immediately and O_NONBLOCK is 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 set errno to [EINTR], but the connection request shall not be aborted, and the connection shall be established asynchronously. so like, rude interruption but the vibe continues.
if the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS], but the connection request shall not be aborted, and the connection shall be established asynchronously. subsequent calls to connect() for the same socket, before the connection is established, shall fail and set errno to [EALREADY]. like, chill, it's already happening. π
when the connection has been established asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for writing. π₯
the socket in use may require the process to have appropriate privileges to use the connect() function. π
π RETURN VALUE π
upon successful completion, connect() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error. that's it. that's the whole thing. slay.
π 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β thesocketargument is not a valid file descriptor.ECONNREFUSEDβ the target address was not listening for connections or refused the connection request. (the classic. π)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β thesocketargument 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 inaddress.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 inaddressnames an existing file that is neither a directory nor a symbolic link to a directory, or the pathname inaddresscontains 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 one whole sentence, i know, breathe. π§)
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β theaddress_lenargument 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 inaddress.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. π (yes, that's a real note in the original, and yes, it means the rest is bonus content.)
πΈ EXAMPLES πΈ
None.
like... none. zero. nothing. the void. π
π¦ 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. don't try to reuse that cursed socket, bestie, it's not coming back.
π RATIONALE π
None.
(again. iconic restraint.)
β FUTURE DIRECTIONS β
None.
(they're not planning anything. periodt.)
π₯ 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 .
ngl if you read all the way to the end of a POSIX manpage you are genuinely built different. main character energy fr fr. π«Ά
and that's a wrap, bestie β this page was brought to you by IEEE/The Open Group, 2017, page CONNECT(3POSIX), and honestly? she ate. π
β¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms