Chapter 6homes.ieu.edu.tr/~skondakci/courses/CE306/Slides/old_slides/w13-s… · Chapter 6....
Transcript of Chapter 6homes.ieu.edu.tr/~skondakci/courses/CE306/Slides/old_slides/w13-s… · Chapter 6....
Transport Layer 3-1
The Transport Layer
Chapter 6
Transport Layer 3-2
Transport services and protocols
� provide logical communicationbetween app processes running on different hosts
� transport protocols run in end systems
� send side: breaks app messages into segments, passes to network layer
� rcv side: reassembles segments into messages, passes to app layer
� more than one transport protocol available to apps
� Internet: TCP and UDP
applicationtransportnetworkdata linkphysical
applicationtransportnetworkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysicalnetwork
data linkphysical
logical end-end transport
Transport Layer 3-3
Transport vs. network layer
� network layer: logical communication between hosts
� transport layer: logical communication between processes � relies on, enhances, network layer services
Household analogy:
12 kids sending letters to 12 kids
� processes = kids
� app messages = letters in envelopes
� hosts = houses
� transport protocol = Ann and Bill
� network-layer protocol = postal service
Transport Layer 3-4
Internet transport-layer protocols
� reliable, in-order delivery (TCP)� congestion control
� flow control
� connection setup
� unreliable, unordered delivery: UDP� no-frills extension of “best-effort” IP
� services not available: � delay guarantees
� bandwidth guarantees
applicationtransportnetworkdata linkphysical
applicationtransportnetworkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysicalnetwork
data linkphysical
logical end-end transport
no-frills:service that is basic and has no luxuries or unnecessary details. Oxford
Transport Layer 3-5
Berkeley Sockets
The socket primitives for TCP.
Transport Layer 3-6
Multiplexing/demultiplexing
application
transport
network
link
physical
P1 application
transport
network
link
physical
application
transport
network
link
physical
P2P3 P4P1
host 1 host 2 host 3
= process= socket
delivering received segmentsto correct socket
Demultiplexing at rcv host:gathering data from multiplesockets, enveloping data with header (later used for demultiplexing)
Multiplexing at send host:
Transport Layer 3-7
How demultiplexing works� host receives IP datagrams
� each datagram has source IP address, destination IP address
� each datagram carries 1 transport-layer segment
� each segment has source, destination port number
� host uses IP addresses & port numbers to direct segment to appropriate socket
source port # dest port #
32 bits
applicationdata
(message)
other header fields
TCP/UDP segment format
Transport Layer 3-8
Connectionless demultiplexing
� Create sockets with port numbers:
� UDP socket identified by two-tuple:
(dest IP address, dest port number)
� When host receives UDP segment:� checks destination port number in segment
� directs UDP segment to socket with that port number
� IP datagrams with different source IP addresses and/or source port numbers directed to same socket
Transport Layer 3-9
Connectionless demux (cont)socket();
recvfrom();
ClientIP:B
P2
clientIP: A
P1P1P3
serverIP: C
SP: 6428
DP: 9157
SP: 9157
DP: 6428
SP: 6428
DP: 5775
SP: 5775
DP: 6428
SP provides “return address”
Transport Layer 3-10
Connection-oriented demux
� TCP socket identified by 4-tuple: � source IP address
� source port number
� dest IP address
� dest port number
� recv host uses all four values to direct segment to appropriate socket
� Server host may support many simultaneous TCP sockets:� each socket identified by its own 4-tuple
� Web servers have different sockets for each connecting client
Transport Layer 3-11
Connection-oriented demux(cont)
ClientIP:B
P1
clientIP: A
P1P2P4
serverIP: C
SP: 9157
DP: 80
SP: 9157
DP: 80
P5 P6 P3
D-IP:C
S-IP: A
D-IP:C
S-IP: B
SP: 5775
DP: 80
D-IP:C
S-IP: B
Transport Layer 3-12
The Transport LayerSocket Programming
Transport Layer 3-13
Berkeley Sockets
The socket primitives for TCP.
Transport Layer 3-14
Transport Layer 3-15
Transport Layer 3-16
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
domain:
AF_INET for network protocol
AF_UNIX for local socket (using a file).
type:
SOCK_STREAM (reliable stream-oriented service or Stream Sockets)
SOCK_DGRAM (best effort, datagram service or Datagram Sockets)
protocol:
IPPROTO_TCP, IPPROTO_UDP
Creates an endpoint for communication and returns a file descriptor
for the socket.
int sd;
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket()
Sample Call:
Transport Layer 3-17
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
#define PORT 0x1234
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; /* Address Family inet */
sin.sin_addr.s_addr = INADDR_ANY; /* Address to accept any incoming con.
sin.sin_port = htons(PORT); /* Host to Network Short */
/* bind the socket to the port number */
bind(sd, (struct sockaddr *) &sin, sizeof(sin))
Bind assigns a socket an address.
bind()
Sample Call:
Transport Layer 3-18
int listen(int sockfd, int backlog);
backlog: an integer representing the number of pending
connections that can be queued up at any one time.
listen(sd, 5)
Listen prepares the socket for incoming connections.
listen()
Sample Call:
Transport Layer 3-19
int accept(int sockfd,
struct sockaddr *serv_addr,
int addrlen);
struct sockaddr_in pin;
addrlen = sizeof(pin);
sd_current = accept(sd, (struct sockaddr *) &pin, &addrlen))
Accept creates a new socket for each connection and removes the connection
from the listen queue
accept()
Sample Call:
Transport Layer 3-20
int send(int sockfd, const void *msg, int len, int flags);
int recv(int sockfd, void *buf, int len, int flags);
These two functions are for communicating over stream sockets or
connected datagram sockets.
send() returns the number of bytes actually sent out
recv() returns the number of bytes actually read into the buffer
/* get a message from the client */
recv(sd_current, dir, sizeof(dir), 0)
/* acknowledge the message, reply w/ the file names */
send(sd_current, dir, strlen(dir), 0)
send() and recv()
Sample Call:
Transport Layer 3-21
int connect(int sockfd,
struct sockaddr *serv_addr,
int addrlen);
The gethostbyname() and gethostbyaddr() functions are
used to resolve host names and addresses in the domain name system
or the local host’s other resolver mechanisms.
struct hostent *gethostbyname(const char *name);
struct hostent *gethostbyaddr(const void *addr,
int len,
int type);
The connect() system call connects a socket, identified by its
file descriptor, to a remote host
connect() , gethostbyname() , gethostbyaddr()
Transport Layer 3-22
#define PORT 0x1234
struct sockaddr_in pin;
/* go find out about the desired host machine */
hp = gethostbyname(hostname)
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(PORT);
/* grab an Internet domain socket */
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
/* connect to PORT on HOST */
connect(sd,(struct sockaddr *) &pin, sizeof(pin))
/* send a message to the server PORT on machine HOST */
send(sd, argv[1], strlen(argv[1]), 0)
/* wait for a message to come back from the server */
recv(sd, dir, DIRSIZE, 0)
close(sd);
Sample Call:
Transport Layer 3-23
int sendto(int sockfd, const void *msg, int len, unsigned int flags,
const struct sockaddr *dest_addr, socklen_t destlen);
int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen);
These two functions are for communicating over connectionless
datagram sockets.
/* get a message from the client */
recvfrom(sd, buf, BUFLEN, 0, &pin, &addrlen)
/* acknowledge the message, reply w/ the file names */
sendto(sd, buf, BUFLEN, 0, &pin, sizeof(pin))
sendto() and recvfrom()
Sample Call:
Transport Layer 3-24
#define PORT 0x1234
struct sockaddr_in pin;
/* go find out about the desired host machine */
hp = gethostbyname(hostname)
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(PORT);
/* grab an Internet domain socket */
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
/* receive a message to come back from the server */
recvfrom(sd, buf, BUFLEN, 0, &pin, sizeof(pin))
close(sd);
Sample Call:
UDP Client Side
Transport Layer 3-25
int shutdown(int sockfd, int how);
close(int sockfd);
Close the connection on your socket descriptor.
shutdown() or close()
how specifies the type of shutdown.
SHUT_RD
Disables further receive operations.
SHUT_WR
Disables further send operations.
SHUT_RDWR
Disables further send and receive operations.
close(sd);
Transport Layer 3-26
send() ≡ sendto() if dest_addr = NULL, destlen = 0
send() ≡ write if no flags is used
recv() ≡ recvfrom() if from = NULL, fromlen = 0
recv() ≡ read() if no flags is used.
• a pointer to a struct sockaddr_in can be cast to a pointer to a
struct sockaddr and vice-versa.
• accept() returns to you a brand new socket file descriptor to use
for the connection that is accepted!
Misc.
Transport Layer 3-27
Socket Programming Example:Internet File Server
Client code using sockets.
6-6-1
Transport Layer 3-28
Socket Programming Example:Internet File Server (2)
Server code using sockets.