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_STREAMorSOCK_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
errnois 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 alsopath_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_rangeinip(7). every seat in the ephemeral cinema is filled ๐ฟEAFNOSUPPORTโ the passed address didn't have the correct address family in itssa_familyfield. 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โ aconnect()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 withEAGAINinstead.) it is possible toselect(2)orpoll(2)for completion by selecting the socket for writing. afterselect(2)indicates writability, usegetsockopt(2)to read theSO_ERRORoption at levelSOL_SOCKETto determine whetherconnect()completed successfully (SO_ERRORis zero) or unsuccessfully (SO_ERRORis 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; seesignal(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 ๐ซถ
ifconnect()fails, consider the state of the socket as unspecified. portable applications shouldclose()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
- Copyright 1993, Rickard E. Faith <faith@cs.unc.edu>
- Copyright 1983, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms