socket programming

Reddit -> Beej's Guide to Network Programming

Beej's Guide to Network Programming

chapter 2 What is a socket?

sockets

a socket is a way for programs to communicate with each other, using standard Unix file descriptors, which is a fancy way to say the integers representing open files. in Unix everything is a file, network connections are also files and thus one can use sockets for communication over network.

one may use socket() to get the file descriptor of a network communication. then communicate through it using send() and recv().

stream sockets (TCP) and datagram sockets (UDP)

there are 2 types of Internet sockets, 'stream sockets' and 'datagram sockets'.

- stream sockets use TCP, which makes sure the data arrives in order and error-free.
- datagram sockets use UDP, where the data is also error-free, but it may arrive, and it may arrive out of order.

in TCP, there is an open connection between two hosts while UDP does not require a connection.

why one use UDP if it is unreliable? because of speed.

but how do one tackle the problem that the packets may not arrive? applications may built their own protocols on top of UDP, for example, implement a checking system where the receiver will send a packet to sender once it receives a packet so that the sender knows the status. if the sender gets no reply for a while, the sender will resend the packet until it receives the confirmation.

encapsulation

when a sender sends data, it will be wrapped/encapsulated in hearders by several protocols. for example, data -> TFTP -> UDP -> IP -> Ethernet. when a receiver receives data, it will be unpacked by several programs.

OSI model

- application
- presentation
- session
- transport
- network
- data link
- physical

chapter 3 IP Addresses, structs, and Data Munging

one can compress zeros in IPv6 address as below:

- 2001:0db8:c9d2:0012:0000:0000:0000:0051
- 2001:db8:c9d2:12::51

- 2001:0db8:ab00:0000:0000:0000:0000:0000
- 2001:db8:ab00::

the IPv6 eqivalence of 127.0.0.1 is ::1.

one can represent IPv4 address in IPv6 as below:

- 192.0.2.33
- ::ffff:192.0.2.33

subnet

an IP address can be splitted into 2 parts: the network portion and the host portion. for example, a IPv4 address 192.0.2.12. we can say the network is 192.0.2.0 and the host is 12.

a netmask describes the network portion of an IP address. for example, 255.255.255.0 means the network portion is the first 24 bits. note another notation for masking 24 bits is 192.0.2.12/24.

port numbers

on top of IP address, TCP and UDP use port number for connection.

chapter 5 System Calls or Bust

- getaddrinfo(), set up the structs for later use.
- socket(), get the file descriptor.
- bind(), associate the socket with a local port.
- connect(), connect to remote host.
- listen(), listen for incoming connections.
- accept(), accept a connection trying to connect the host listening port. it will return a new socket for this connection.
- send() and recv(), communicate between host and remote.
- sendto() and recvfrom(), same as above, but for datagram sockets.
- close(), shutdown(), close the connection of a socket, the latter one can do one-way close.

chapter 7 Slightly Advanced Techniques

blocking

blocking: fancy word for sleep.

when one run listener, it calls recvfrom(), receives no data, and the socket will be blocked until there is data.

one can toggle between block and non-block mode (default).

`poll()`

`select()`

用咗 Neat CSS 嚟整,呢度嘅所有文字係根據 CC BY-SA 4.0 嘅條款發佈。