CONNECT(2)
β¨ CONNECT(2) β¨
okay bestie, welcome to the System Calls Manual page for connect π this is the one that slides into another socket's DMs. no cap.
πΈ NAME πΈ
connect β initiate a connection on a socket
π LIBRARY π
Standard C Library (libc, -lc) π«Ά
π SYNOPSIS π
#include <sys/socket.h>
int
connect(int s, const struct sockaddr *name, socklen_t namelen);
π DESCRIPTION π
so like, s is a socket, right? and what happens next depends entirely on its vibe:
- if
sis typeSOCK_DGRAM, this call is basically you saying "this is my person now π" β it specifies the peer the socket is to be associated with. that address is where datagrams get sent, and it's the only address from which datagrams are received. loyal. monogamous. iconic. - if
sis typeSOCK_STREAM, this call attempts to make a connection to another socket.
the other socket is specified by name, which is an address in the communications space of the socket. namelen tells you how much space name points to, in bytes β and heads up, the sa_len member of name is ignored π (yes literally, it just does not get read).
each communications space interprets the name argument its own way, because of course they do, everyone's got main character energy in their own address family.
generally speaking:
- stream sockets may successfully
connect()only once. one shot. periodt. - datagram sockets may use
connect()multiple times to change their association. serial dater behavior, honestly. - datagram sockets may dissolve the association by connecting to an invalid address, such as a null address. it's giving "we're not together anymore π".
π¦ RETURN VALUES π¦
connect() returns the value 0 if successful β slay β¨ β otherwise the value -1 is returned and the global variable errno is set to indicate the error. so if you get -1, check errno, bestie, it's telling you exactly what went wrong π.
π₯ ERRORS π₯
the connect() system call fails if:
[EBADF]β Thesargument is not a valid descriptor. that's not a socket, babe, that's just... a number.[EINVAL]β Thenamelenargument is not a valid length for the address family.[ENOTSOCK]β Thesargument is a descriptor for a file, not a socket. you brought a book to a phone call ππ΅.[EADDRNOTAVAIL]β The specified address is not available on this machine.[EAFNOSUPPORT]β Addresses in the specified address family cannot be used with this socket.[EISCONN]β The socket is already connected. bestie you're literally already taken.[ETIMEDOUT]β Connection establishment timed out without establishing a connection. left on read, fr fr π.[ECONNREFUSED]β The attempt to connect was forcefully rejected. oof.[ECONNRESET]β The connection was reset by the remote host.[ENETUNREACH]β The network is not reachable from this host.[EHOSTUNREACH]β The remote host is not reachable from this host.[EADDRINUSE]β The address is already in use.[EFAULT]β Thenameargument specifies an area outside the process address space.[EINPROGRESS]β The socket is non-blocking and the connection cannot be completed immediately. it is possible toselect(2)for completion by selecting the socket for writing. it's not done yet, it's just... doing its thing β¨.[EINTR]β The connection attempt was interrupted by the delivery of a signal. the connection will be established in the background, as in the case ofEINPROGRESS. it kept going without you, lowkey.[EALREADY]β A previous connection attempt has not yet been completed.[EACCES]β An attempt is made to connect to a broadcast address (obtained through theINADDR_BROADCASTconstant or theINADDR_NONEreturn value) through a socket that does not provide broadcast functionality. read the room, that socket can't do that.[EAGAIN]β An auto-assigned port number was requested but no auto-assigned ports are available. Increasing the port range specified bysysctl(3)MIB variablesnet.inet.ip.portrange.firstandnet.inet.ip.portrange.lastmay alleviate the problem. π that's your fix, go get it.
π UNIX domain specific errors π
The following errors are specific to connecting names in the UNIX domain. These errors may not apply in future versions of the UNIX IPC domain β so like, they're on the way out, noted π.
[ENOTDIR]β A component of the path prefix is not a directory.[ENAMETOOLONG]β A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. that's a lot of characters, diva.[ENOENT]β The named socket does not exist.[EACCES]β Search permission is denied for a component of the path prefix.[EACCES]β Write access to the named socket is denied.[ELOOP]β Too many symbolic links were encountered in translating the pathname. links inside links inside links π.[EPERM]β Write access to the named socket is denied.
π SEE ALSO π
accept(2), getpeername(2), getsockname(2), select(2), socket(2), sysctl(3), sysctl(8)
β HISTORY β
The connect() system call appeared in 4.2BSD. a true original, an OG, the blueprint π
and that's the whole page, bestie!! this manual page came to you from Debian, dated August 18, 2016 β and fr fr, go forth and connect() responsibly π«ΆπΈβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms