Sockets API and HTTP 1.
0
Prepared by Ruchir Khaitan
Edited by Jae Woo Lee
Copyright notice: care has been taken to use only those web images deemed by the instructor to be in the public domain. If you
see a copyrighted image on any slide and are the copyright owner, please contact the instructor. It will be removed.
The client / server model
Server waits for
incoming requests
over the network from
clients
e.g. Web browser &
web server
C programs use
Sockets API
What is a socket?
End-point for interprocess
communication over
TCP/IP network
Socket is bound to an
IP address and a port
number
Sockets API Summary
Source: [Link]
socket()
Called by both
client and server
On the server, a
listening socket is
created first using
socket() a
connected socket
will be created
later by accept()
bind()
Usually called
only by server
Binds the
listening socket to
a specific port
that should be
known to the
client
listen()
Called only by
server
Sets up the
listening socket to
accept
connections
accept()
Called only by
server
By default blocks
until a connection
request arrives
Creates and returns
a new socket for
each new client
Listening socket vs. Connected socket
connect
accept
Source: [Link]
send() and recv()
Called by both client
and server
Reads and writes to
the other side
Message
boundaries may not
be preserved
HTTP 1.0
Client sends a HTTP request for a resource
on the server (ex. a file on the server)
The server sends a HTTP response
HTTP request
First line: method, request-URI, version
Ex: GET /[Link] HTTP/1.0\r\n
Followed by 0 or more headers:
Ex: Host: [Link]\r\n
Followed by an empty line
\r\n
HTTP request example
HTTP response
First line: response status
o
o
Success: HTTP/1.0 200 OK\r\n
Failure: HTTP/1.0 404 Not Found\r\n
Followed by 0 or more response headers
Followed by a blank line
Followed by the content of the response
o
For example, an image file or an HTML file
HTTP response example