Chapter 2 Application layer r 2 1 Principles

  • Slides: 26
Download presentation
Chapter 2: Application layer r 2. 1 Principles of r r network applications 2.

Chapter 2: Application layer r 2. 1 Principles of r r network applications 2. 2 Web and HTTP Internet gaming 2. 3 FTP 2. 4 Electronic Mail v SMTP, POP 3, IMAP r 2. 5 DNS r 2. 6 P 2 P file sharing r VOIP r 2. 8 Socket programming with TCP r 2. 9 Socket programming with UDP r 2. 10 Building a Web server 2: Application Layer 1

Definition v v also called IP Telephony, Internet telephony, Broadband Phone and Voice over

Definition v v also called IP Telephony, Internet 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 7941 G 2: Application Layer 2

Big Picture r Modes of operation: v PC to PC v PC to phone

Big Picture r Modes of operation: v PC to PC v PC to phone v Phone to PC v Phone to Phone r Traffic go through Packet Switched Network instead of Public Switched Telephone Network (PSTN) From Wikipedia, the free encyclopedia 2: Application Layer 3

Challenges r Quality of Service (Qo. S) v Internet provides best of service v

Challenges r Quality of Service (Qo. S) v Internet provides best of service v No guarantee for latency, jitter… r Need Internet connection v Home broadband is not reliable r Power issue v VOIP phone, Cable Modem/DSL, Computer 2: Application Layer 4

Challenges r Security v Most unencrypted v VOIP spam challenges r Integration into global

Challenges r Security v Most unencrypted v VOIP spam challenges r Integration into global telephone number system r Emergency call availability & functionality Power, Internet connection v Call routing, location service v 2: Application Layer 5

Qo. S r Deal with Jitter v Smoothed by playback buffer r Bandwidth v

Qo. S r Deal with Jitter v Smoothed by playback buffer r Bandwidth v 64 kbps or less v Depends on codec and use of silence supression 2: Application Layer 6

Chapter 2: Application layer r 2. 1 Principles of r r network applications 2.

Chapter 2: Application layer r 2. 1 Principles of r r network applications 2. 2 Web and HTTP Internet gaming 2. 3 FTP 2. 4 Electronic Mail v r 2. 6 P 2 P file sharing r VOIP r 2. 7 Socket programming with TCP r 2. 8 Socket programming with UDP SMTP, POP 3, IMAP r 2. 5 DNS 2: Application Layer 7

Socket programming Goal: learn how to build client/server application that communicate using sockets Socket

Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API r introduced in BSD 4. 1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API: v v unreliable datagram (UDP) reliable, byte stream-oriented (TCP) 2: Application Layer 8

Socket-programming using TCP Socket: an interface between application process and end-transport protocol (UCP or

Socket-programming using TCP Socket: an interface between application process and end-transport protocol (UCP or TCP) Why socket? : A Layer seen by application, OS transparent controlled by application developer controlled by operating system 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 r server process must first be

Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that accepts client’s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client v allows server to talk with multiple clients v 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 r Unix socket (berkeley socket) r Winsock r Mac.

Many Versions of Socket APIs r Unix socket (berkeley socket) r Winsock r Mac. TCP r …. r We introduce Unix socket API here v Can program under SUN OS, Linux, etc v 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 4 Family: AF_INET Service:

Socket Descriptor Data Structure Descriptor Table 0 1 2 3 4 Family: AF_INET Service: SOCK_STREAM Local IP: 111. 22. 3. 4 Remote IP: 123. 45. 6. 78 Local Port: 2249 Remote Port: 3726 2: Application Layer 12

TCP Client/Server Socket Overview TCP Server socket() TCP Client bind() socket() listen() bind() connect()

TCP Client/Server Socket Overview TCP Server socket() TCP Client bind() socket() listen() bind() connect() send() connection establishment data request accept() recv() data reply send() recv() close() end-of-file notification recv() close() 2: Application Layer 13

What is a Socket? int sockfd; /* socket descriptor */ if ((sockfd = socket(AF_INET,

What is a Socket? int sockfd; /* socket descriptor */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) } perror(“socket”); exit(1); } r socket returns an integer (socket descriptor) sockfd < 0 indicates that an error occurred v socket descriptors are similar to file descriptors v r AF_INET: associates a socket with the Internet protocol family r SOCK_STREAM: selects the TCP protocol r SOCK_DGRAM: selects the UDP protocol 2: Application Layer 14

Socket Structure (Client) AF_INET struct sockaddr_in { short int sin_family; // Address family unsigned

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 1 A 2 D 3 C 4 B … IP: 1 A. 2 D. 3 C. 4 B Big-Endian (Network Byte Order) 2: Application Layer 15

Bind (Client) int sockfd; struct sockaddr_in local_addr; local_addr. sin_family = AF_INET; local_addr. sin_port =

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(&(my_addr. sin_zero), '', 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 16

Remote Host Structure Longwood. cs. ucf. edu struct hostent { char *h_name; /* official

Remote Host Structure Longwood. cs. ucf. edu struct hostent { char *h_name; /* official name */ char **h_aliases; /* alias list */ mail. cs. ucf. edu int h_addrtype; /* address type */ int h_length; /* address length */ char **h_addr_list; /* address list */ }; #define h_addr_list[0] /* backward compatibility */ hostent *hp; “ 132. 170. 108. 1” hp = gethostbyname(“mail. cs. ucf. edu”); struct sockaddr_in remote_addr; remote_addr. sin_family = AF_INET; remote_addr. sin_port = htons(80); // short, network byte order (big-endian) remote_addr. sin_addr = *((struct in_addr *)hp->h_addr); memset(&(remote_addr. sin_zero), '', 8); // zero the rest Remote host info 2: Application Layer 17

Connect(), send(), recv() by Client connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr); Local host socket

Connect(), send(), recv() by Client connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr); Local host socket Remote host info Struct sockaddr_in After connecting to the remote sever…. char send. Str[100], recv. Str[100]; …. send(sockfd, send. Str, strlen(send. Str), 0); … recv. Num. Byte = recv(sockfd, recv. Str, Max. Data. Size, 0); close(sockfd); Blocking call 2: Application Layer 18

Partial Send() and recv() Due to multiple packets in transmission #include <sys/types. h> #include

Partial Send() and recv() Due to multiple packets in transmission #include <sys/types. h> #include <sys/socket. h> int sendall(int s, 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(s, 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 19

Socket Programming in Server r No need to connect() a remote host r Need

Socket Programming in Server r No need to connect() a remote host r Need to listen() on specified port r Accept() a connection request v Generate a new socket for one connection • Support multiple connections listen(sockfd, back. Log); // back. Log is the number of connections in queue new_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sizeof(struct sockaddr_in)) New socket discriptor Following commun. through this 2: Application Layer 20

Socket Programming in Server: fork() for multi-connection service while(1) { // main accept() loop

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); printf("server: got connection from %sn", inet_ntoa(remote_addr. sin_addr)); if (!fork()) { // this is the child process (child process ID=0) close(sockfd); // child doesn't need the listener send(new_fd, "Hello, world!n", 14, 0); ……… close(new_fd); exit(0); } close(new_fd); // parent doesn't need this …………. } 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. 2: Application Layer 21

Chapter 2: Application layer r 2. 1 Principles of network applications r 2. 2

Chapter 2: Application layer r 2. 1 Principles of network applications r 2. 2 Web and HTTP r 2. 3 FTP r 2. 4 Electronic Mail v SMTP, POP 3, IMAP r 2. 5 DNS r 2. 6 P 2 P file sharing r 2. 7 VOIP r 2. 8 Socket programming with TCP r 2. 9 Socket programming with UDP r 2. 10 Building a Web server 2: Application Layer 22

Socket programming with UDP: no “connection” between client and server r no handshaking r

Socket programming with UDP: no “connection” between client and server r no handshaking r sender explicitly attaches IP address and port of destination to each packet r 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 23

UDP Socket Programming r sockfd = socket(AF_INET, SOCK_DGRAM, 0) SOCK_STREAM (tcp) r No connect(),

UDP Socket Programming r sockfd = socket(AF_INET, SOCK_DGRAM, 0) SOCK_STREAM (tcp) r No connect(), accept() r Send() sendto(), recv() recvfrom() v Sendto() includes target address/port 2: Application Layer 24

Chapter 2: Summary Our study of network apps now complete! r Application architectures v

Chapter 2: Summary Our study of network apps now complete! r Application architectures v client-server v P 2 P v hybrid r application service requirements: v reliability, bandwidth, delay r Internet transport service model v v connection-oriented, reliable: TCP unreliable, datagrams: UDP r specific protocols: v HTTP v FTP v SMTP, POP, IMAP v DNS r Some applications v Web v Email v DNS v Internet gaming, VOIP v P 2 P r socket programming 2: Application Layer 25

Chapter 2: Summary Most importantly: learned about protocols r typical request/reply message exchange: v

Chapter 2: Summary Most importantly: learned about protocols r typical request/reply message exchange: v v client requests info or service server responds with data, status code r message formats: v headers: fields giving info about data v data: info being communicated r 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” v r r 2: Application Layer 26