Transcript Chapter2

Chapter 2: Application layer
 2.1 Principles of




network applications
2.2 Web and HTTP
Internet gaming
2.3 FTP
2.4 Electronic Mail

SMTP, POP3, IMAP
 2.5 DNS
 2.6 P2P file sharing
 VOIP
 2.8 Socket programming
with TCP
 2.9 Socket programming
with UDP
 2.10 Building a Web
server
2: Application Layer
1
Definition


also called IP Telephony, Internet telephony, Broadband
telephony, Broadband Phone and Voice over Broadband
the routing of voice conversations over the Internet or
through any other IP-based network
Cisco IP Phone 7941G
2: Application Layer
2
Big Picture
 Modes of operation:
 PC to PC
 PC to phone
 Phone to PC
 Phone to Phone
 Traffic go through Packet
Switched Network instead of
Public Switched Telephone
Network (PSTN)
From Wikipedia, the free encyclopedia
2: Application Layer
3
Challenges
 Quality of Service (QoS)
 Internet provides best of service
 No guarantee for latency, jitter…
 Need Internet connection
 Home broadband is not reliable
 Power issue
 VOIP phone, Cable Modem/DSL, Computer
 Primary reason for not using VOIP for
emergency calls
• Second reason is location identification is hard for
VOIP
2: Application Layer
4
Challenges
 Security
Most unencrypted
 VOIP spam challenges

 Integration into global telephone number
system
 Emergency call availability & functionality
Power, Internet connection
 Call routing, location service

2: Application Layer
5
QoS
 Deal with Jitter
 Smoothed by playback buffer
 Will cause more delay in playback
 Too much delayed packets will be discard (dropped)
 Bandwidth
 64 kbps or less
 Depends on codec and use of silence suppression
2: Application Layer
6
Chapter 2: Application layer
 2.1 Principles of




network applications
2.2 Web and HTTP
Internet gaming
2.3 FTP
2.4 Electronic Mail

 2.6 P2P file sharing
 VOIP
 2.7 Socket programming
with TCP
 2.8 Socket programming
with UDP
SMTP, POP3, IMAP
 2.5 DNS
2: Application Layer
7
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
Socket API
 introduced in BSD4.1 UNIX, 1981
 explicitly created, used, released by apps
 client/server paradigm
 two types of transport service via socket API:


unreliable datagram (UDP)
reliable, byte stream-oriented (TCP)
2: Application Layer
8
Socket-programming using TCP
Socket: an interface between application process and
end-end-transport protocol (UCP or TCP)
Why socket?: A Layer seen by application, OS
transparent
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
host or
server
2: Application Layer
9
Socket programming with TCP
Client must contact server
 server process must first
be running
 server must have created
socket (door) that accepts
client’s contact
Client contacts server by:
 creating client-local TCP
socket
 specifying IP address, port
number of server process
 When client creates
socket: client TCP
establishes connection to
server TCP
 When contacted by client,
server TCP creates new
socket for server process to
communicate with client
 allows server to talk with
multiple clients
 source port numbers
used to distinguish
clients (more in Chap 3)
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
2: Application Layer
10
Many Versions of Socket APIs
 Unix socket (berkeley socket)
 Winsock
 MacTCP
 ….
 We introduce Unix socket API here
 Can program under SUN OS, Linux, etc
 A good tutorial on socket programming:
• http://beej.us/guide/bgnet/
2: Application Layer
11
Socket Descriptor Data
Structure
Descriptor Table
0
1
2
3
Family: AF_INET
Service: SOCK_STREAM
Local IP: 111.22.3.4
Remote IP: 123.45.6.78
Local Port: 2249
Remote Port: 3726
4
2: Application Layer
12
TCP Client/Server Socket
Overview
TCP Server
socket()
TCP Client
bind()
socket()
listen()
bind()
connect()
send()
accept()
connection establishment
data request
recv()
data reply
send()
recv()
close()
end-of-file notification
recv()
close()
2: Application Layer
13
C Programming Header Files
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <netdb.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <unistd.h>

 Enough for our socket programming in our
Unix server, maybe not enough for other
2: Application Layer
computers
14
What is a Socket?
int sockfd;
/* socket descriptor */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(“socket”); exit(1);
}
 socket returns an integer (socket descriptor)
sockfd < 0 indicates that an error occurred
 socket descriptors are similar to file descriptors

• FILE *fid; fid=fopen(“test.txt”, “rt”);
 AF_INET: associates a socket with the Internet
protocol family
 SOCK_STREAM: selects the TCP protocol
 SOCK_DGRAM: selects the UDP protocol
2: Application Layer
15
Socket Structure (Client)
AF_INET
struct sockaddr_in {
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // all zero
};
// Internet address (Network Byte Order)
// (a structure for historical reasons)
struct in_addr {
unsigned long s_addr;
// that's a 32-bit long, or 4 bytes
};
…
100
101
102
103
1A
2D
3C
4B
…
IP: 1A.2D.3C.4B
Big-Endian (Network Byte Order)
2: Application Layer
16
Bind (Client)
int sockfd;
struct sockaddr_in local_addr;
local_addr.sin_family = AF_INET;
local_addr.sin_port = 0; // random assign a port
local_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address
memset(&(local_addr.sin_zero), '\0', 8); // zero the rest of the struct
Local
host
info
sockfd = socket(AF_INET, SOCK_STREAM, 0); // create an empty socket
bind(sockfd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr));
2: Application Layer
17
Remote Host Structure
feather.cs.ucf.edu
struct hostent {
char *h_name; /* official name */
char **h_aliases; /* alias list */ www.cs.ucf.edu
int
h_addrtype; /* address type */
int
h_length; /* address length */
char **h_addr_list; /* address list */
};
#define h_addr h_addr_list[0] /* backward compatibility */
struct hostent *hp;
“10.173.204.109”
hp = gethostbyname(“www.cs.ucf.edu”);
struct sockaddr_in remote_addr;
Remote
remote_addr.sin_family = AF_INET;
host
remote_addr.sin_port = htons(80); // short, network byte order (big-endian)
info
remote_addr.sin_addr = *((struct in_addr *)hp->h_addr);
memset(&(remote_addr.sin_zero), '\0', 8); // zero the rest
2: Application Layer
18
Connect(), send(), recv() by Client
connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr);
Local host
socket
Remote host
info
Struct sockaddr  sockaddr_in
After connecting to the remote sever….
Blocking call
char sendStr[100], recvStr[100];
….
numByteSend=send(sockfd, sendStr, sendByteNum), 0);
…
recvNumByte = recv(sockfd, recvStr, MaxDataSize, 0);
close(sockfd);
2: Application Layer
19
Partial Send() and recv()
Due to multiple packets in transmission
#include <sys/types.h>
#include <sys/socket.h>
int sendall(int sockfd, char *buf, int *len) {
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = send(sockfd, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n; bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
2: Application Layer
20
Socket Programming in Server
 Still need to initialize local_addr struct
(page 17)
 No need to initialize remote_addr struct
(page 18)
 No need to connect() a remote host

Instead, need to listen() on specified port
(server port)
 Accept() a connection request
 Generate a new socket for one connection
• Support multiple connections
2: Application Layer
21
int sockfd, new_fd; struct sockaddr_in local_addr, remote_addr;
assign local_addr (see Page 17. Use hton() to assign server port!)
socket(…); // create empty socket descriptor
bind(…); //fill in local address and assigned port to the socket descriptor
listen(sockfd, backLog); // backLog is the max no. of connections in queue
new_fd = accept(sockfd, (struct sockaddr *)&remote_addr,
&sizeof(struct sockaddr_in))
New socket discriptor
Following commun. through this
char sendStr[100], recvStr[100];
….
recvNumByte = recv(new_fd, recvStr, MaxDataSize, 0);
…..
numByteSend=send(new_fd, sendStr, sendByteNum), 0);
…
close(new_fd);
2: Application Layer
close(sockfd);
22
Socket Programming in Server:
fork() for multi-connection service
while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size);
parent printf("server: got connection from %s\n", inet_ntoa(remote_addr.sin_addr));
if (!fork()) { // this is the child process (fork() returns 0 in child process)
close(sockfd); // child doesn't need the listener
child
send(new_fd, "Hello, world!\n", 14, 0); ………
close(new_fd);
exit(0);
}
parent close(new_fd); // parent doesn't need this
………….
}
2: Application Layer
23
Fork()
 Tuotrial on fork():
http://www.erlenstar.demon.co.uk/unix/faq_2.html
 System call fork() is used to create child process.
It returns a process ID. After a new child process
is created, both processes will execute the next
instruction following the fork() system call.
 On success:


PID of the child process is returned in the parent's
thread of execution
0 is returned in the child's thread of execution
2: Application Layer
24
Chapter 2: Application layer
 2.1 Principles of
network applications
 2.2 Web and HTTP
 2.3 FTP
 2.4 Electronic Mail

SMTP, POP3, IMAP
 2.5 DNS
 2.6 P2P file sharing
 2.7 VOIP
 2.8 Socket programming
with TCP
 2.9 Socket programming
with UDP
 2.10 Building a Web
server
2: Application Layer
25
Socket programming with UDP
UDP: no “connection” between
client and server
 no handshaking
 sender explicitly attaches
IP address and port of
destination to each packet
 server must extract IP
address, port of sender
from received packet
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
UDP: transmitted data may be
received out of order, or
lost
2: Application Layer
26
UDP Socket Programming
 sockfd = socket(AF_INET, SOCK_DGRAM, 0)
SOCK_STREAM (tcp)
 No connect(), accept()
 Send()  sendto(), recv()  recvfrom()
 Sendto() includes target address/port
2: Application Layer
27
Chapter 2: Summary
Our study of network apps now complete!
 Application architectures
 client-server
 P2P
 hybrid
 application service
requirements:

reliability, bandwidth,
delay
 Internet transport
service model


connection-oriented,
reliable: TCP
unreliable, datagrams: UDP
 specific protocols:
 HTTP
 FTP
 SMTP, POP, IMAP
 DNS
 Some applications
 Web
 Email
 DNS
 Internet gaming, VOIP
 P2P
 socket programming
2: Application Layer
28
Chapter 2: Summary
Most importantly: learned about protocols
 typical request/reply
message exchange:


client requests info or
service
server responds with
data, status code
 message formats:
 headers: fields giving
info about data
 data: info being
communicated
 control vs. data msgs
in-band, out-of-band
(ftp)
centralized vs. decentralized
stateless vs. stateful
reliable vs. unreliable msg
transfer
“complexity at network
edge”





2: Application Layer
29