socket_connect4(3)

πŸ“¦
version 0.34 Β· 2 versions
  • 0.34 Β· alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.30 Β· fedora 43, fedora 44, ubuntu jammy
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– socket_connect4 β€” bestie's guide to saying "hey" over TCP πŸ’–

okay so you wanna know about socket_connect4, from the Library Functions Manual, section 3. It's a libowfat thing and honestly? It's giving "let me introduce you to somebody." Sit down, hydrate, let's go. 🫢


✨ NAME ✨

socket_connect4 β€” attempt to make a TCP connection πŸ’…


🌸 SYNTAX 🌸

#include <libowfat/socket.h>

int socket_connect4(int s,const char ip[4],uint16 port);

no cap, that's the whole signature. s is your TCP socket, ip is a 4-byte address, port is a uint16. that's it bestie. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

So like β€” socket_connect4 attempts to make a connection from TCP socket s to TCP port port on IP address ip. Simple. Iconic. A matchmaker for sockets. πŸ”₯

Now the part you actually need to internalise: socket_connect4 may return a few different things, and each one is a whole mood. πŸ’€

  • 0 β€” this means the connection succeeded, and it succeeded immediately if the socket is non-blocking. slay.
  • -1, with errno set to error_inprogress or error_wouldblock β€” this is the "it's not a no, it's a not-yet" outcome. It means the socket is non-blocking. the connection is still out there doing its thing.
  • -1, with errno set to something else β€” this means the connection failed (and it failed immediately, if the socket is non-blocking). πŸ’”
Heads up bestie: those last two both look like -1 from the outside, so the way you tell "still cooking" apart from "it's over" is by checking errno. Don't skip that step, I'm begging. πŸ™ˆ

When a background connection succeeds or fails, s becomes writable. So that's your cue to check in. You can use socket_connected to see whether the connection actually succeeded. And if the connection failed, socket_connected returns 0, setting errno appropriately. ✨

Once a TCP socket is connected, you can use the read and write system calls to transmit data. yep, the normal ones. the classics. πŸ‘‘

And here's a cute lil flex: you can call socket_connect4 without calling socket_bind4. That has the effect as first calling socket_bind4 with IP address 0.0.0.0 and port 0. So like, the universe just handles it for you. πŸ’–


🌈 EXAMPLE 🌈

copy this, it's cute:

#include <libowfat/socket.h>

int s;
char ip[4];
uint16 p;

s = socket_tcp4();
socket_bind4(s,ip,p);
socket_connect4(s,ip,p);

πŸ‘‘ SEE ALSO πŸ‘‘


okay so that's the whole page, mwah πŸ’œ This one is socket_connect4(3) β€” section 3 of the Library Functions Manual β€” and that's genuinely the whole footer situation, no version, no date stamp, so that's the receipt. go forth and connect, bestie, you're gonna eat πŸ’…βœ¨πŸŽ€