qlibs:(socket_setup)

πŸ“¦ version 31, in alpine 3.24

πŸ’– NAME πŸ’–

socket_setup β€” listen to or accept socket for incoming TCP connections

okay so welcome to the qlibs:(socket_setup) page, bestie πŸ’… this is the one about getting your TCP socket ready to listen and then actually accepting the connection. no cap, it's giving networking main character energy πŸ¦‹


🌸 SYNTAX 🌸

#include "socket_if.h"

int socket_listen(int s,int n);

int socket_accept(int s,char ip[16],
                  uint16 *port,uint32 *scope_id);

int socket_ipoptionskill(int s);

int socket_ip6anycast((int s);

int socket_dualstack(int s);

int socket_nodualstack(int s);

✨ DESCRIPTION ✨

  • socket_listen β€” prepares TCP socket s to accept TCP connections. It allows a backlog of approximately n TCP SYNs. On systems supporting SYN cookies, the backlog is irrelevant β€” iconic caveat, bestie ✨
  • socket_accept β€” accepts the connection. It creates a new socket for the connection and returns a file descriptor pointing to the new socket; you can use the read and write system calls to transmit data through that file descriptor. It also gives you info about the client's ip address and TCP port number, perhaps together with local receiving interface scope_id πŸ’–
  • socket_ipoptionskill β€” used to disable previously defined options in IPv4 or IPv6 packets like Source Routing prior to using this socket for data exchange. It uses the setsockopt πŸŽ€
  • socket_ip6anycast β€” enables unspecified reversed anycasting on the listening socket s with IPv6 address ::. Upon receiving IPv6 packets, the socket records the incoming IPv6 address and the receiving scope_id in order to provide additional routing information πŸ¦‹
  • socket_dualstack and socket_nodualstack β€” can be used to force or forbid dual-stack behavior by setting the setsockopt variable IPV6_V6ONLY appropriately. In the last case, a potential servers needs two instances to accept incoming IPv6 and IPv6 packets β€” yes, that's exactly the energy of that line, bestie πŸ‘€

πŸ”₯ RETURN CODES πŸ”₯

Normally socket_listen, socket_accept and socket_ipotionskill β€” yep, that's the exact spelling in this line of the source, typo and all 😭 β€” as well as socket_dualstack and socket_nodualstack return 0. If anything goes wrong, they return -1, setting errno appropriately.


πŸŽ€ EXAMPLE πŸŽ€

here's the example, copied exactly, no edits, no extra sparkle inside the code block πŸ’…

         #include <socket_if.h>

         int s, t;
         int r;
         char ip[16];
         uint16 p;

         if ((s = socket_tcp()) == -1)
           err_tmp("",111,"unable to create TCP socket: ");
         r = socket_ipoptionskill(s);
         if (socket_bind_reuse(s,(char *)V6localnet,8002,0) == -1)
           err_tmp("",111,"unable to bind: ");
         if (socket_listen(s,1) == -1)
           err_tmp("",111,"unable to listen: ");

         t = socket_tcp();
         socket_bind(t,ip,p,0);
         socket_listen(s,16);
         socket_accept(t,ip,&p,&scope_id);

πŸ¦‹ SEE ALSO πŸ¦‹

go read these too, bestie: socket_if(3), socket_bind(3), socket_connect(3), socket_info(3), socket_recv(3), socket_send(3), socket_tcp(3), socket_udp(3)


bestie, that's the whole qlibs:(socket_setup) page β€” source/version: qlibs, section 3; date: the original footer does not list one, so no invented date here, just vibes 🫢✨

πŸ“¦ where this page comes from