connect(2)

๐Ÿ’… connect(2) โ€” System Calls Manual, but make it a group chat ๐Ÿ’…

okay bestie sit DOWN because today we're learning about connect(2), a member of the System Calls Manual (section 2, the syscall girlies โœจ). it's literally the moment your socket slides into someone's DMs. iconic behavior only.


๐ŸŒธ NAME ๐ŸŒธ

connect โ€” initiate a connection on a socket ๐Ÿ’Œ


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” the classic, the blueprint, the moment ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <sys/socket.h>

int connect(int sockfd, const struct sockaddr *addr,
            socklen_t addrlen);

three args, three chances to fumble the bag. let's go ๐Ÿฆ‹


๐ŸŽ€ DESCRIPTION ๐ŸŽ€

so the connect() system call does the thing where it takes the socket that the file descriptor sockfd points at and connects it to the address specified by addr ๐Ÿซถ. the addrlen argument says how big addr is. and the format of the address sitting in addr? that's decided by the address space of the socket sockfd โ€” peek socket(2) for the full tea, no cap.

now, vibe check on socket types because behavior is giving different depending on what you are:

  • if sockfd is type SOCK_DGRAM, then addr is the address datagrams get sent to by default, and it's also the only address datagrams are received from. exclusive much? ๐Ÿ’…
  • if the socket is type SOCK_STREAM or SOCK_SEQPACKET, this call attempts to make a connection to the socket that is bound to the address specified by addr. periodt.

some protocol sockets (think UNIX domain stream sockets, bestie) may successfully connect() only once. one shot, no take-backs ๐Ÿ˜ญ.

some other protocol sockets (looking at you, datagram sockets in the UNIX and Internet domains) may use connect() multiple times to change their association. flexible. we love range ๐ŸŒˆ.

and some protocol sockets (TCP sockets, plus datagram sockets in the UNIX and Internet domains) may dissolve the association by connecting to an address whose sa_family member of sockaddr is set to AF_UNSPEC. after that little breakup, the socket can be connected to another address. also fun fact: AF_UNSPEC has been supported since Linux 2.2 โญ.


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • if the connection or binding succeeds โ†’ zero is returned. slay. ๐Ÿ’š
  • on error โ†’ -1 is returned, and errno is set to indicate the error. not slay, but informative. ๐Ÿ’€

๐Ÿ’€ ERRORS ๐Ÿ’€

heads up: these are the general socket errors only. there may be other, domain-specific error codes lurking. don't be surprised by extras.

  • EACCES โ€” for UNIX domain sockets that are identified by pathname: write permission is denied on the socket file, or search permission is denied for one of the directories in the path prefix. (see also path_resolution(7).) basically you weren't on the guest list.
  • EACCES, EPERM โ€” the user tried to connect to a broadcast address without having the socket broadcast flag enabled, or the connection request failed because of a local firewall rule. the firewall said no ๐Ÿšซ.
  • EACCES โ€” it can also be returned if an SELinux policy denied a connection (for example, if there's a policy saying an HTTP proxy can only connect to ports associated with HTTP servers, and the proxy tries to connect to a different port). SELinux is that strict friend who checks your outfit before you leave the house.
  • EADDRINUSE โ€” local address is already in use. it's taken, babe ๐Ÿ˜ญ
  • EADDRNOTAVAIL โ€” (Internet domain sockets) the socket referred to by sockfd had not previously been bound to an address, and upon attempting to bind it to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. see the discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7). every seat in the ephemeral cinema is filled ๐Ÿฟ
  • EAFNOSUPPORT โ€” the passed address didn't have the correct address family in its sa_family field. wrong fam, fr fr.
  • EAGAIN โ€” for nonblocking UNIX domain sockets: the socket is nonblocking, and the connection cannot be completed immediately. for other socket families: there are insufficient entries in the routing cache.
  • EALREADY โ€” the socket is nonblocking and a previous connection attempt has not yet been completed. patience, bestie ๐Ÿงš
  • EBADF โ€” sockfd is not a valid open file descriptor. that's not a real fd, that's a fake id ๐Ÿ’€
  • ECONNREFUSED โ€” a connect() on a stream socket found no one listening on the remote address. nobody home. read receipt never came. ๐Ÿ˜ญ
  • EFAULT โ€” the socket structure address is outside the user's address space. that memory isn't yours, girl ๐Ÿ™ˆ
  • EINPROGRESS โ€” the socket is nonblocking and the connection cannot be completed immediately. (UNIX domain sockets failed with EAGAIN instead.) it is possible to select(2) or poll(2) for completion by selecting the socket for writing. after select(2) 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 here, explaining the reason for the failure). it's giving "loadingโ€ฆ" spinner energy โณ
  • EINTR โ€” the system call was interrupted by a signal that was caught; see signal(7).
  • EISCONN โ€” the socket is already connected. you're literally already in the relationship ๐Ÿคจ
  • ENETUNREACH โ€” network is unreachable. the wifi said bye ๐ŸŒ
  • ENOTSOCK โ€” the file descriptor sockfd does not refer to a socket. sir, that is not a socket. that is a random fd.
  • EPROTOTYPE โ€” the socket type does not support the requested communications protocol. this error can occur, for example, on an attempt to connect a UNIX domain datagram socket to a stream socket. mismatched vibes, incompatible ships ๐Ÿซ 
  • ETIMEDOUT โ€” timeout while attempting connection. the server may be too busy to accept new connections. note that for IP sockets the timeout may be very long when syncookies are enabled on the server. just sitting there, waiting, staring at the screen ๐Ÿ˜ถโ€๐ŸŒซ๏ธ

๐Ÿฆ‹ VERSIONS ๐Ÿฆ‹

portable programs must ensure that addr.sun_path is a null-terminated string for AF_UNIX sockets. no half-finished strings, bestie, tie it off ๐Ÿ’…


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2024. certified, official, canon. โœจ


๐ŸŒˆ HISTORY ๐ŸŒˆ

POSIX.1-2001, SVr4, 4.2BSD. this call is a legacy icon and it ate ๐Ÿ’…


๐Ÿซถ NOTES ๐Ÿซถ

if connect() fails, consider the state of the socket as unspecified. portable applications should close() the socket and create a new one for reconnecting.

no cap: don't try to reuse a burned socket, just build a new one. clean slate era. ๐Ÿ”ฅ


๐ŸŒธ EXAMPLES ๐ŸŒธ

an example of the use of connect() is shown in getaddrinfo(3). go read it, it's a good time ๐Ÿ’–


๐ŸŽ€ SEE ALSO ๐ŸŽ€

accept(2), bind(2), getsockname(2), listen(2), socket(2), path_resolution(7), selinux(8) โ€” the whole friend group, go say hi ๐Ÿซถ


okay that's the whole page, bestie!! thank you for coming to my ted talk ๐Ÿ’œ this lil fanfic was brought to you by Linux man-pages 6.20, dated 2026-02-08, and yes, that's the same connect(2) you know and love. now go forth and connect responsibly ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from
  • 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 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms