- Clients and servers use the socket function to create a socket descriptor:
int socket(int domain, int type, int protocol)- Example:
int clientfd = socket(AF_INET, SOCK_STREAM, 0);- which indicates that using 32-bit IPV4 addresses and the socket will be the end point of a reliable TCP connection
- Better Example:
struct addrinfo *infop = ...; int sockfd = socket(p->ai_family, p-> ai_socktype, p->ai_protocol);- which is not protocol specific
- A server uses
bindto ask the kernel to associate the server's socket address with a socket descriptorint bind(int sockfd, SA *addr, socklen_t addrlen);typedef struct sockaddr SA;
- Process can read bytes that arrive on the connection whose endpoint is
addrby reading from descriptorsockfd - Similarly, writes to
sockfdare transferred along connection whose endpoint isaddr
- Kernel assumes that descriptor from socket function is an active socket that will be on the client end
- A server calls the listen function to tell the kernel that a descriptor will be used by a server rather than a client:
int listen(int sockfd, int backlog);
- Converts
sockfdfrom an active socket to a listening socket that can accept connection requests from clients backlogis a hint about the number of outstanding connection requests that the kernel should queue up before starting to refuse requests (128-ish by default)
- Servers wait for connection requests from clients by calling
accept:int accept(int listenfd, SA *addr, int *addrlen);
- Waits for connection request to arrive on the connection bound to
listenfd, then fills in client’s socket address in addr and size of the socket address inaddrlen - Returns a connected descriptor that can be used to communicate with the client via Unix I/O routines
- A client establishes a connection with a server by calling connect:
int connect(int clientfd, SA *addr, socklen_t addrlen);
- Attempts to establish a connection with server at socket address
addr- If successful, then
clientfdis now ready for reading and writing - Resulting connection is characterized by socket pair
- (x:y, addr.sin_addr:addr.sin_port)
- x is client address
- y is ephemeral port that uniquely identifies client process on client host
- If successful, then
- Best practice is to use getaddrinfo to supply the arguments
addrandaddrlen
- Listening descriptor
- End point for client connection requests
- Created once and exists for lifetime of the server
- Connected descriptor
- End point of the connection between client and server
- A new descriptor is created each time the server accepts a connection request from a client
- Exists only as long as it taks to service client
- This allows for concurrent servers that can communicate over many client connections simultaneously
- Clients and servers communicate using the HyperText Transfer Protocol (HTTP)
- Client and server establish TCP connection
- Client requests content
- Server responds with requested content
- Client and server close connection (eventually)
- Web servers return content to clients
- content: a sequence of bytes with an associated MIME (Multipurpose Internet Mail Extensions) type
- text/html; text/plain; iimage/gif; mage/png; image/jpeg;
- content: a sequence of bytes with an associated MIME (Multipurpose Internet Mail Extensions) type
- The content returned in HTTP responses can be either static or dynamic
- Static content: content stored in files and retrieved in response to an HTTP request
- Dynamic content: content produced on-the-fly in response to an HTTP request
- Unique name for a file: URL (Universal Resource Locator)
- Clients use prefix (http://www.cmu.edu:80) to infer:
- What kind (protocol) of server to contact (HTTP)
- Where the server is (www.cmu.edu)
- What port it is listening on (80)
- Servers use suffix (/index.html) to:
- Determine if request is for static or dynamic content.
- One convention: executables reside in
cgi-bindirectory
- One convention: executables reside in
- Find file on file system
- Determine if request is for static or dynamic content.
- HTTP request is a request line, followed by zero or more request headers
- Request line:
- is one of GET, POST, OPTIONS, HEAD, PUT, DELETE, or TRACE
- is typically URL for proxies, URL suffix for servers
- is HTTP version of request (HTTP/1.0 or HTTP/1.1)
- Request headers: :
- Provide additional information to the server
- HTTP response is a response line followed by zero or more response headers, possibly followed by content, with blank line (“\r\n”) separating headers from content
- Response line:
- is HTTP version of the response
- is numeric status
- is corresponding English text
- 200 OK Request was handled without error
- 301 Moved Provide alternate URL
- 404 Not foound Server couldn't find the file
- Response headers: :
- Content-Type: MIME type of content in response body
- Content-Length: Length of content in response body
- CGI defines a simple standard for transferring information between the client (browser), the server, and the child process
- CGI is the original standard for generating dynamic content
- Has been largely replaced by other, faster techniques:
- E.g., fastCGI, Apache modules, Java servlets, Rails controllers
- Avoid having to create process on the fly (expensive and slow).
- Has been largely replaced by other, faster techniques:
- How does the client pass arguments to the server?
- The arguments are appended to the URI
- http://add.com/cgi-bin/adder?15213&18213
- adder is the CGI program on the server that will do the addition.
- argument list starts with “?”
- arguments separated by “&”
- spaces represented by “+” or “%20”
- The arguments are appended to the URI
- How does the server pass these arguments to the child?
- In environment variable QUERY_STRING
- A single string containing everything after the “?”
- In environment variable QUERY_STRING
- How does the server capture the content produced by the child?
- The child generates its output on stdout and server uses
dup2to redirect stdout to its connected socket
- The child generates its output on stdout and server uses
- Notice that only the CGI child process knows the content type and length, so it must generate those headers



