Sockets PDF
Sockets PDF
Sockets PDF
Communication
Jeff Chase
Duke University
Services
Do A for me.
OK, heres your answer.
Now do B.
OK, here.
Client
Server
An Internet Application
Internet client host
Client
User code
Server
TCP/IP
Kernel code
TCP/IP
Sockets interface
(system calls)
Hardware interface
(interrupts)
Network
adapter
Hardware
and firmware
Network
adapter
Global IP Internet
[CMU 15-213]
Networking Basics
Applications Layer
Standard apps
HTTP
FTP
Telnet
User apps
Transport Layer
TCP
UDP
Programming Interface:
Sockets
Network Layer
IP
Link Layer
Device drivers
Application
(http,ftp,telnet,)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
[Buyya]
[CMU 15-213]
Internet Connections
Most clients and servers communicate by sending streams of
bytes over connections
E.g., using TCP, the Transmission Control Protocol
A socket is an endpoint of a connection between two processes.
Unix and Windows system calls, Java APIs
socket
Client
socket
Server
Client
Server
(port 80)
Server host address
208.216.181.15
[CMU 15-213]
Client
Client
(connect request)
(connect request)
Web server
(port 80)
Kernel
Echo server
(port 7)
Web server
(port 80)
Kernel
Echo server
(port 7)
[CMU 15-213]
More on Ports
This port abstraction is an Internet Protocol concept.
Source/dest port is named in every packet.
Kernel looks at port to demultiplex incoming traffic.
The term is commonly used to refer to a communication
endpoint in other contexts.
How do clients know what port number to connect to?
We have to agree on well-known ports for common
services: ICAAN again
Look at /etc/services
Ports 1023 and below are reserved
Clients need a return port, but it can be an ephemeral
port assigned dynamically by the kernel.
Berkeley Sockets
Networking protocols are implemented as part of the
OS
The networking API exported by most OSs is the
socket interface
Unix Sockets I
Creating a socket
int socket(int domain, int type, int protocol)
domain = AF_INET, AF_UNIX
type = SOCK_STREAM, SOCK_DGRAM
What is this
integer that is
returned?
kernel
file
pipe
process file
descriptor
table
socket
system open file
table
tty
Disclaimer:
this drawing is
oversimplified.
Sending/Receiving
Use read/write system calls and variants to
transmit/receive byte-stream data.
Just like files!
Close works too
Alternative syscalls for sending/receiving messages
Variants of:
int send(int socket, char *msg, int mlen, int flags)
int recv(int socket, char *buf, int blen, int flags)
port
server
Connection request
Client
[Buyya]
Making a Connection
If everything goes well, the server accepts the
connection.
Upon acceptance, the server gets a new socket bound
to a different port.
It needs a new socket (consequently a different port number) so
that it can continue to listen to the original socket for connection
requests while serving the connected client.
port
port
port
server
Client
Connection
[Buyya]
Server-Side Sockets
Bind socket to IP address/port
int bind(int socket, struct sockaddr *addr, int addr_len)
Client Socket
Active Open (on client)
int connect(int socket, struct sockaddr *addr,
int addr_len)
Connection-oriented example
(TCP)
Server
Socket()
Bind()
Client
Listen()
Socket()
Accept()
Connection Establishmt.
Block until
connect
Recv()
Process
request
Send()
Data (request)
Connect()
Send()
Data (reply)
Recv()
[Paul Barford]
Connectionless example
(UDP)
Server
Socket()
Client
Bind()
Socket()
Recvfrom()
Bind()
Block until
Data from
client
Data (request)
Sendto()
Process
request
Sendto()
Data (reply)
Recvfrom()
[Paul Barford]
Socket call
Means by which an application attached to the network
int socket(int family, int type, int protocol)
Family: address family (protocol family)
AF_UNIX, AF_INET, AF_NS, AF_IMPLINK
Type: semantics of communication
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
Not all combinations of family and type are valid
Protocol: Usually set to 0 but can be set to specific value.
Family and type usually imply the protocol
Return value is a handle for new socket
[Paul Barford]
Bind call
Binds a newly created socket to the specified address
Int bind(int socket, struct sockaddr *address, int addr_len)
[Paul Barford]
Listen call
Used by connection-oriented servers to indicate an
application is willing to receive connections
Int(int socket, int backlog)
Socket: handle of newly creates socket
Backlog: number of connection requests that can be
queued by the system while waiting for server to
execute accept call.
[Paul Barford]
Accept call
After executing listen, the accept call carries out a
passive open (server prepared to accept connects).
Int accept(int socket, struct sockaddr *address, int addr_len)
[Paul Barford]
Connect call
Client executes an active open of a connection
Int connect(int socket, struct sockaddr *address, int addr_len)
[Paul Barford]
Send(to), Recv(from)
After connection has been made, application uses
send/recv to data
Int send(int socket, char *message, int msg_len, int flags)
Send specified message using specified socket
Int recv(int scoket, char *buffer, int buf_len, int flags)
Receive message from specified socket into specified buffer
[Paul Barford]
[Buyya]
[Buyya]
[Buyya]
[Buyya]
[Buyya]
In Action
http://www-cse.ucsd.edu
HTTP
Client
Server
HTTP in a Nutshell
GET /path/to/file/index.html HTTP/1.0
Content-type: MIME/html, Content-Length: 5000,...
Client
Server
execute
program
Content-type: MIME/html, Content-Length: 5000,...
Client
Server
Web Servers
Clients and servers communicate
using the HyperText Transfer
Protocol (HTTP)
Client and server establish
HTTP request
TCP connection
Web
Web
client
Client requests content
server
(browser)
Server responds with
HTTP response
requested content
(content)
Client and server close
connection (usually)
E.g., HTTP/1.1
IETF RFC 2616, June, 1999.
[CMU 15-213]
Web Content
Web servers return content to clients
content: a sequence of bytes with an associated MIME
(Multipurpose Internet Mail Extensions) type
Example MIME types
text/html
HTML document
text/plain
Unformatted text
application/postscript Postcript document
image/gif
Binary image encoded in GIF format
image/jpeg
Binary image in JPEG format
[CMU 15-213]
[CMU 15-213]
URLs
Each file managed by a server has a unique name called a URL
(Universal Resource Locator)
URLs for static content:
http://www.cs.cmu.edu:80/index.html
http://www.cs.cmu.edu/index.html
http://www.cs.cmu.edu
Identifies a file called index.html, managed by a Web
server at www.cs.cmu.edu that is listening on port 80.
URLs for dynamic content:
http://www.cs.cmu.edu:8000/cgi-bin/adder?15000&213
Identifies an executable file called adder, managed by a
Web server at www.cs.cmu.edu that is listening on port
8000, that should be called with two argument strings: 15000
and 213.
[CMU 15-213]
Anatomy of an HTTP
Transaction
unix> telnet www.aol.com 80
Trying 205.188.146.23...
Connected to aol.com.
Escape character is '^]'.
GET / HTTP/1.1
host: www.aol.com
HTTP/1.0 200 OK
MIME-Version: 1.0
Date: Mon, 08 Jan 2001 04:59:42 GMT
Server: NaviServer/2.0 AOLserver/2.3.3
Content-Type: text/html
Server: expect HTML in the response body
Content-Length: 42092
Server: expect 42,092 bytes in the resp body
Server: empty line (\r\n) terminates hdrs
<html>
Server: first HTML line in response body
...
Server: 766 lines of HTML not shown.
</html>
Server: last HTML line in response body
Connection closed by foreign host. Server: closes connection
unix>
Client: closes connection and terminates
[CMU 15-213]
HTTP Requests
HTTP request is a request line, followed by zero or
more request headers
Request line: <method> <uri> <version>
<version> is HTTP version of request (HTTP/1.0 or
HTTP/1.1)
<uri> is typically URL for proxies, URL suffix for servers.
A URL is a type of URI (Uniform Resource Identifier)
See http://www.ietf.org/rfc/rfc2396.txt
<method> is either GET, POST, OPTIONS, HEAD, PUT,
DELETE, or TRACE.
[CMU 15-213]
HTTP Responses
HTTP response is a response line followed by zero or more response
headers.
Response line:
HTTP Server
HTTP Server
Creates a socket (socket)
Binds to an address
Listens to setup accept backlog
Can call accept to block waiting for connections
(Can call select to check for data on multiple socks)
Handle request
GET /index.html HTTP/1.0\n
<optional body, multiple lines>\n
\n
accept
queue
packet
queues
listen
queue
Measures
offered load
response time
throughput
utilization