lec 17: network programming

today’s plan

DNS & HTTP

programming abstraction: socket

(today: client-side socket)

(later this week: server-side socket & case study - node.js)

big picture

what happens when browsing http://www.cs.uw.edu/

protocol: HTTP

domain name: www.cs.uw.edu

(IP address: 128.208.3.118)

port: 80 (default)

q: can we directly use 128.208.3.118 in browser?

Internet protocol suite (TCP/IP)

[see also: OSI model]

protocol examples

application layer: DNS, HTTP, SSH, …

transport layer: TCP, UDP, …

internet layer: IPv4, IPv6, …

overview

translate www.cs.uw.edu to 128.208.3.118 (DNS)

connect to 128.208.3.118 (TCP)

send request to fetch data (HTTP)

DNS

a naming system (recall file system)

map domain names to IP addresses

example: www.cs.uw.edu128.208.3.118

q: can one domain name map to multiple IP addresses?

q: can multiple domain names map to the same IP address?

DNS resolution

a single file (HOSTS.TXT or /etc/hosts)? scalability?

hierarchy

root: .

generic top-level domain (gTLD): edu

[draw google.com, mit.edu]

root servers

13 logical root servers

run: dig . ns

demo: resolve www.cs.uw.edu

q: where’s mit.edu (from Seattle)?

programming interface

manpage: getaddrinfo, freeaddrinfo, getnameinfo

example: dns.c

more on DNS

MIT 6.033’s DNS hands-on

take CSE 461

what’s next

now we have the IP address

how to connect to the server and fetch data?

socket programming

client-side socket

// return a socket (file descriptor)
int socket(int domain, int type, int protocol);
// connect to a server
int connect(int fd, const struct sockaddr *addr, socklen_t addrlen);

use socket/connect to start a client-side socket (not open)

you can use read/write on sockets (or recv/send)

close when done

example: http.c

HTTP protocol

request/response protocol, over TCP, default port 80

client (e.g., browser) sends a request

server produces a response

HTTP/1.1 textual (HTTP/2 binary)

request format

[method] [uri] HTTP/[version]\r\n
[name]: [value]\r\n
...
[name]: [value]\r\n
\r\n
[body]

methods: GET, POST, HEAD, …

demo (chrome & httpie): http -v www.cs.uw.edu

response format

HTTP/[version] [status code] [reasons]\r\n
[name]: [value]\r\n
...
[name]: [value]\r\n
\r\n
[body]

status code

1xx: some kind of informational message

2xx: success of some kind

3xx: redirects the client to a different URL

4xx: the client’s request contained some error

5xx: the server experienced an error

see you on Wednesday

server-side socket