Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
application layer
application layer
User Process
Internet
Socket
transport layer (TCP/UDP)
network layer (IP)
link layer (e.g. ethernet)
User Process
Socket
transport layer (TCP/UDP)
Internet
Internet
Operating system
Deliver data to the destination socket
Based on the destination port number (e.g., 80)
Application
Read data from and write data to the socket
Interpret the data (e.g., render a Web page)
3
User process
socket
socket
Operating
System
Operating
System
4
Postal Mail
Single
to receive
letters
Singlemailbox
mailbox
to receive
messages
Unreliable
Unreliable
Not necessarily in-order
Not necessarily in-order delivery
delivery
Each
is independent
Lettersletter
sent independently
Must address each reply
Telephone
Postal Mail
Call
Connection-oriented single
socket per connection
Setup connection followed by
data transfer
Guaranteed
delivery
Single mailbox
to receive
messages
In-order
delivery
Unreliable
Socket Identification
Communication Protocol
TCP (Stream Socket): streaming, reliable
UDP (Datagram Socket): packets, best effort
Receiving host
Destination address that uniquely identifies the host
An IP address is a 32-bit quantity
Receiving socket
Host may be running many different processes
Destination port that uniquely identifies the socket
A port number is a 16-bit quantity
8
port Y
TCP/UDP
IP
Port Number
Protocol
Host Address
Ethernet Adapter
9
Server program
GET /index.html
10
Client-Server Communication
Client sometimes on
Initiates a request to the
server when interested
E.g., Web browser on your
laptop or cell phone
Doesnt communicate
directly with other clients
Needs to know servers
address
Server is always on
Handles services requests
from many client hosts
E.g., Web server for the
www.cnn.com Web site
Doesnt initiate contact with
the clients
Needs fixed, known address
11
Server Process
12
Web server
(port 80)
OS
Client
Echo server
(port 7)
Web server
(port 80)
OS
Echo server
(port 7)
14
Client-Server Communication
Stream Sockets (TCP): Connection-oriented
Server
Create a socket
Bind the socket
(what port am I on?)
Client
Listen for client
(Wait for incoming connections)
Accept connection
Receive Request
Send response
Create a socket
Connect to server
Receive response
15
Client-Server Communication
Datagram Sockets (UDP): Connectionless
Server
Client
Create a socket
Create a socket
Bind the socket
Receive Request
Send response
Receive response
16
Connection-oriented Example
(Stream Sockets -TCP)
Server
socket()
bind()
listen()
Client
socket()
connect()
accept()
recv()
send()
send()
recv()
18
Connectionless Example
(Datagram Sockets - UDP)
Server
socket()
bind()
Client
socket()
bind()
sendto()
recvfrom()
sendto()
recvfrom()
19
char *service
struct addrinfo *hints,
struct addrinfo **result)
ai_flags;
ai_family; //e.g. AF_INET for IPv4
ai_socketype; //e.g. SOCK_STREAM for TCP
ai_protocol; //e.g. IPPROTO_TCP
ai_addrlen;
*ai_canonname;
*ai_addr; // point to sockaddr struct
*ai_next;
Example
hints.ai_family = AF_UNSPEC;
// don't care IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
int status = getaddrinfo("www.cnn.com", 80", &hints, &result);
// result now points to a linked list of 1 or more addrinfos
// etc.
21
UNSPEC: unspecified
(PF_INET and SOCK_STREAM already implies TCP)
Example
sockfd = socket(result->ai_family,
result->ai_socktype,
result->ai_protocol);
22
E.g. connect(
sockfd,
result->ai_addr,
result->ai_addrlen);
23
24
25
Create a socket
26
27
int
accept(int
sockfd,
30
31
http://www.mozilla.org/developer/
Sendmail
http://www.sendmail.org/
32