hw 3 due tomorrow
ex15 due Friday
struct addrinfo {
int ai_flags; /* input flags */
int ai_family; /* protocol family for socket */
int ai_socktype; /* socket type */
...
};
/* designated initializer: C only */
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
/* C/C++ */
struct addrinfo hints = {0};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
review client sockets
server sockets
client: read
file from disk & write
data to socket (ex15)
applications don’t worry about packet reordering/loss using TCP
client-side write
ensures data reaches server?
server-side write
ensures data reaches disk?
N - data reaches a kernel buffer when write
returns
DNS: getaddrinfo
/freeaddrinfo
socket
, connect
, close
read
(or recv
), write
(or send
)
imagine a Q&A session with a group of people
step 1: find a place to stand (so people can see you)
step 2: say you’re ready to go
step 3: take a question from the audience
/* bind a name to a socket */
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
/* listen for connections */
int listen(int sockfd, int backlog);
/* accept a connection */
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
port < 1024 requires root (linux CAP_NET_BIND_SERVICE)
SO_REUSEADDR
int listen(int sockfd, int backlog);
backlog
: hint for the number of pending connections
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
addr
, addrlen
: client address
pass NULL if you don’t need them
our echo server: one connection at a time
how to talk to multiple clients at the same time?
send a file over network & bypass userspace
write
with mmap
sendfile
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
how to decide the length of request/response: close the connection, Content-Length field, or chunked transfer encoding
one HTTP request outstanding per TCP connection
server push
see HTTP/2