<aside> π Basics
</aside>
IP:port
pair, and uses standard Unix file descriptors behind the scenes<aside> π Types of Sockets
</aside>
There are a lot of sockets, such as Internet Sockets (DARPA INTERNET), UNIX sockets, X.25 sockets etc.
SOCK_STREAM
) and Datagram sockets (SOCK_DGRAM
)SOCK_STREAM
are reliable and use TCP behind the scenes, while on the other hand, SOCK_DGRAM
are fast and use UDP behind the scenesSOCK_DGRAM
we don't have to maintain an open connection, like that in case of `SOCK_STREAM<aside> π How are sockets used?
</aside>
146.86.5.20
and 161.25.19.8
) are communicating with one another, with ports 1625
and 80
respectively, then the sockets in communication can be said as 146.86.5.20:1625
and 146.86.5.20:80
<aside> π Implementing sockets
</aside>
socket()
system routine, which returns a socket file descriptor in return. We can then communicate with the descriptor using send()
and recv()
, sentto()
and revcfrom()
for SOCK_DGRAM.read()
and write()
system calls with the socket file descriptor, but the former ones offer much greater control over the socket.<aside> π Socket file descriptor
</aside>
Itβs simply an integer.
<aside>
π struct addrinfo
</aside>
struct addrinfo {
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname
struct addrinfo *ai_next; // linked list, next node
};