We focus on TCP in this class.
socket
creates an endpoint for communication and returns a file descriptor that refers to that endpoint.
bind
associates an abstract network socket created with socket
to an actual networking interface/address.
listen
marks a socket as “passive”, or ready to accept incoming connections.
accept
accepts an incoming connection on a socket that has been listening.
connect
is used to connect a socket to a given address (which might have a socket listening for connections).
Make sure to check errors for every call, networking can fail at any point! All networking functions specify in their manpages what types of errors you might have to deal with.
man 3 htonl
for more info.setsockopt
to set SO_REUSEADDR
on your socket.int is_running = 1;
int handler(){
is_running = 0;
}
int main(){
while(is_running){ /*...*/};
close(...);
}