qlibs:(socket_connect)

πŸ“¦ version 31, in alpine 3.24

πŸ’– qlibs:(socket_connect) β€” manual section 3, bestie πŸ’–

🌸 NAME 🌸

okay so socket_connect is the one that initiates or tests a socket connection to a remote IPv4/IPv6 address. literally the β€œcan we talk?” of sockets. πŸ’…

✨ SYNTAX ✨

First, include this:

#include "socket_if.h"

Then the signatures, exactly:

int socket_connect4(int s,const char ip[4],uint16 port);
int socket_connect6(int s,const char ip[16],uint16 port,
                    uint32 scope_id);
int socket_connect(int s,const char ip[16],uint16 port,
                   uint32 scope_id);

int socket_connected(int s);

πŸ¦‹ DESCRIPTION πŸ¦‹

socket_connect4 tries to make a connection from TCP or UDP socket s to TCP port port on IP address ip. You can call socket_connect4 without calling socket_bind4 first β€” that’s giving β€œfirst call socket_bind4 with IP address 0.0.0.0 and port 0” energy. ✨

socket_connect6 tries to make a connection from TCP or UDP socket s to TCP port port on IP address ip and scope_id. The meaning of scope_id depends on the implementation and IPv6 IP. For link-local IPv6 addresses it specifies the outgoing interface index. From a given interface name (e.g. "eth0") its index can be retrieved with socket_getifidx. scope_id should normally be set to 0. You can call socket_connect6 without calling socket_bind6 first β€” that’s the same effect as first calling socket_bind6 with IP address :: and port 0. πŸŽ€

socket_connect tries to make a connection from TCP socket s to TCP port port on IP address ip and scope_id by calling socket_connect6. If, however, ip is an IPv4 or IPv4-mapped IPv6 address, socket_connect4 is called instead. iykyk. πŸ‘‘

Once a socket is connected, you can use the read and write system calls to transmit data. slay. πŸ’–

socket_connected can be used to verify whether a background connection failed or succeeded β€” thus whether s became writable or not. vibe check for your socket. 🌈

πŸ“ EXAMPLE πŸ“

         #include <socket_if.h>

         int s;
         char localip[16];
         char remoteip[16];
         uint16 p = 0;

         s = socket_tcp();
         socket_bind(s,localip,p,0);
         socket_connect(s,remoteip,p,0);

         if (socket_connected(s) != 1)
           err_tmp(""111,fatal,"unable to setup TCP connection: ");

πŸ”₯ RETURN CODES πŸ”₯

  • socket_connect4, socket_connect6 and socket_connect β€” may return 0 to indicate that the connection succeeded (and succeeded immediately, if the socket is non-blocking). πŸŽ‰
  • They may return -1, setting errno to error_inprogress or error_wouldblock, to indicate that the socket is non-blocking. 🫢
  • They may return -1, setting errno to something else, to indicate that the connection failed (and failed immediately, if the socket is non-blocking). 😭

socket_connected returns 1 if s is a socket and a connection is established, 0 otherwise and setting errno appropriately. no cap. πŸ’―

πŸ’œ SEE ALSO πŸ’œ


byeee bestie β€” this page is manual section 3, source/version qlibs:(socket_connect), and that’s the footer tea πŸ«ΆπŸ’…βœ¨

πŸ“¦ where this page comes from