Network Programming
Network Programming
What is a socket:
a socket is a way to speak to other programs using standard Unix file descriptors. When Unix programs
do any sort of I/O operation, they do it by reading or writing to a file descriptor. A file descriptor is
simply an integer associated with an open file, and that file can be a network connection, a FIFO, a
pipe, a terminal and so on. To get a file descriptor for network communication, you make a call to the
socket() system routine, that returns the socket descriptor, and you communicate through it using the
specialized send() and recv() socket calls.
There are many type of sockets depending on which Unix flavour you are on, but the one I’ll be
studying is internet sockets.
Data encapsulation
When a packet is born, it is wrapped in an header (and sometimes also a footer) by the first protocol
(say the tftp) protocol, then the whole thing is encapsulated again by the next protocol (say, UDP), then
again by the next one (IP), and then again by the final one on the hardware layer (say, Ethernet).