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 s is type SOCK_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 s is type SOCK_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] β€” The s argument is not a valid descriptor. that's not a socket, babe, that's just... a number.
  • [EINVAL] β€” The namelen argument is not a valid length for the address family.
  • [ENOTSOCK] β€” The s argument 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] β€” The name argument specifies an area outside the process address space.
  • [EINPROGRESS] β€” The socket is non-blocking and the connection cannot be completed immediately. it is possible to select(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 of EINPROGRESS. 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 the INADDR_BROADCAST constant or the INADDR_NONE return 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 by sysctl(3) MIB variables net.inet.ip.portrange.first and net.inet.ip.portrange.last may 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