- mp0 intro to socket programming
- mp1 http server
- mp2 tcp
- mp3 routing algorithm
- mp4 csma
Remember, any thing is a file under Unix.
- On every step do error checking on return value and
perror()it. getaddrinfo()Fill thestruct addrinfosocket()Get the file descriptorbind()Associate the socket with a port on your local machineconnect()Connect to a remote host.listen()Wait for incoming connections and setbacklogaccept()Handle the connections and get new socket descriptor(fork a new socket for every connection).send()andrecv()Talk to me, baby and be careful about the return value.sendto()andrecvfrom()Talk to me, DGRAM-style.Used for unconnected datagram socketsclose()andshutdown()Shutdown()change socket's usability whileclose()free a socket descriptor.getpeername()andgethostname()Who are you? Who am I?
| struct | function | notes |
|---|---|---|
struct addrinfo |
getaddrinfo() and freeaddrinfo() |
struct sockaddr *ai_addr |
struct sockaddr |
can be cast into/back sockaddr_in or sockaddr_in6 |
|
struct sockaddr_in |
inet_pton(AF_INET, ipv4 string, struct sockaddr_in *) and its brother inet_ntop |
struct in_addr sin_addr |
struct sockaddr_in6 |
inet_pton(AF_INET6, ipv6 string, struct sockaddr_in6 *) and its brother inet_ntop |
struct in6_addr sin6_addr |
struct sockaddr_storage |
check ss_familt and cast it to according socket address struct |
- If you see something like this:
Makefile:66: *** missing separator. Stop.Makefile expect command is indented by tab other than space, so you maybe use \t or use tab. - TCP and UDP can use same port.
- Use strlen() instead of sizeof().
- CRLF = CR LF
- HTTP Response
- Request line, such as GET /logo.gif HTTP/1.1 or Status line, such as HTTP/1.1 200 OK,
- Headers
- An empty line
- Optional HTTP message body data
The request/status line and headers must all end with <CR><LF> (that is, a carriage return followed by a line feed). The empty line must consist of only and no other whitespace.
- Don't set const too small