Lecture 4: Application layer (socket API)

Download Report

Transcript Lecture 4: Application layer (socket API)

• Review:
– Why layer architecture?
– peer entities
– Protocol and service interface
– Connection-oriented/connectionless service
– Reliable/unreliable service
– What type of service will a typical end user want?
• Why even considering other types of services
– layers in the TCP/IP reference model
– layers in the ISO/OSI reference model
Application Layer
• Problems to be addressed in this layer:
– Directly related to the individual application (no common
problems)
• Design issues
– directly related to each application (no common issues).
• Introduce application layer concepts.
• Examine the service interface between the application layer
and the transport layer (socket API)
• Give an example showing how to develop network
applications.
Some network apps
•
•
•
•
•
•
E-mail
Web
Instant messaging
Remote login
P2P file sharing
Multi-user network
games
• Streaming stored
video clips
• Internet telephone
• Real-time video
conference
• Massive parallel
computing
Network applications
– run on different end
systems (not routers)
– communicate over a
network.
– view the network as the
abstraction provided by
the network (the transport
layer).
• Addressing end points
• End-to end communication
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
• Service provided by the network:
– Objective: to allow processes on different machines to
talk to each other.
• Is IP address alone sufficient to address an application entity?
– Network end points:
• IP address + port number
– Why not IP address + PID?
• A process can be associated with an end point.
– E.g: http -- 80, ssh – 22, telnet—23, DNS – 53
– Service provided by the network: end-to-end
communications:
• Reliable connection-oriented byte stream service (TCP)
• Unreliable connectionless service (UDP)
– From a network application point of view, what is the logical
topology of the network?
• A process can bind to an end point and talk to anyone else in the
network.
• TCP API:
– How to access the end point from a process:
– socket: create a new communication end point
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
domain: AF_UNIX
file system
AF_INET
internet address
type: SOCK_STREAM
reliable connect-oriented, byte
stream
SOCK_DGRAM
unreliable connectionless
SOCK_SEQPACKET record stream
protocol: 0, non-zero for a specific protocol
– bind: attach an address to a socket
int bind(int socket, const struct sockaddr *address, size_t
address_len)
address: contains the port number, address.sin_port
• Service primitives for TCP:
– listen: announce willingness to accept connections
int listen(int socket, int backlog);
backlog: number of outstanding connections in the listen queue
– accpet: accept a new connection on a socket
int accept (int socket, struct sockaddr *address, size_t *address_len);
address: the address of the connecting socket
– connect: try to establish a connection
int connect(int socket, const struct sockaddr *address, size_t
address_len);
address: the destination address
– write: send data
ssize_t write(int fildes, const void *buf, size_t nbyte);
– read: receive data
ssize_t read(int fildes, void *buf, size_t nbyte);
– close: close a connection
int close(int fildes);
Server:
socket
bind
listen
accept
read/write
close
Client:
socket
connect
read/write
close
What is the end point (IP + port number) for the server?
What is the end point (IP + port number) for the client?
When is the TCP connection established?
• One more problem: machines may represent
numbers in different ways.
– See example3.c on linprog and program.
– use htons/htonl/ntohl/ntohs routines to solve the
problem.
• See example1.c and example2.c.
• TCP:
– Reliable byte stream service.
• UDP:
– Unreliable datagram service.
• Data may get lost – application may need to deal
with more details in the communication.
• Basic UDP service interface:
– Socket, bind, sendto, recvfrom, close
UDP server
UDP client
TCP server
TCP client
socket
bind
socket
socket
Bind
Listen
Accept
socket
connect
recvfrom
sendto
sendto
recvfrom
close
Read/write
close
read/write
close