libsocket
#Overview
libsocket
is meant to work exactly the same as the regular socket APIs when
using it under IPv4 and UDP. However, under the hood, it uses UNIX sockets to
communicate. As such, the only supported address is 127.0.0.1
(or
localhost
). Placing any other address in s_sendto
does nothing. s_recvfrom
will always return an address from 127.0.0.1
.
If you follow the Socket Programming Tips for UDP, the only change
you’ll need to make is to replace the socket
, bind
, recvfrom
, sendto
calls with ones prepended with s_
.
#s_socket
int s_socket(int domain, int type, int protocol);
#Parameters
int domain
: Only supportsAF_INET
(IPv4).int type
: Only supportsSOCK_DGRAM
(connectionless datagrams).
#Returns
int
: Returns the file descriptor of the created socket or -1
if an error
occured. Check errno
for the specific error.
#s_bind
int s_bind(int sock, const struct sockaddr* addr, socklen_t len);
#Parameters
int sock
: Socket to bind address toconst struct sockaddr* addr
: Only supports addresses of typesockaddr_in
.socklen_t len
: Unused; here for compatibility with the standard socket API.
#Returns
int
: Returns 0
on success or -1
if an error
occured. Check errno
for the specific error.
#s_recvfrom
ssize_t s_recvfrom(int sock, void* buf, size_t len, int flags, struct sockaddr* addr, socklen_t* addrlen);
#Parameters
int sock
: Socket to bind address tovoid* buf
: Buffer to write data tosize_t len
: Max size of buffer in bytesint flags
: See the man page forrecvfrom
struct sockaddr* addr
: Will write out the address the data was received from (always127.0.0.1
+ port).socklen_t addrlen
: Unused; here for compatibility with the standard socket API.
#Returns
ssize_t
: Returns the number of bytes received or -1
if an error
occured. Check errno
for the specific error.
#s_sendto
ssize_t s_sendto(int sock, const void* buf, size_t len, int flags, const struct sockaddr* addr, socklen_t addrlen);
#Parameters
int sock
: Socket to bind address tovoid* buf
: Buffer to read data fromsize_t len
: Size of data to read in bytesint flags
: See the man page forrecvfrom
struct sockaddr* addr
: Only supportssockaddr_in
addresses. Thein_addr
field is ignored.socklen_t addrlen
: Unused; here for compatibility with the standard socket API.
#Returns
ssize_t
: Returns the number of bytes sent or -1
if an error
occured. Check errno
for the specific error.