2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic Mail SMTP, POP3, IMAP r 2.5 DNS r 2.6 P2P 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 2 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 3 Big Picture r Modes of operation: PC to PC PC to phone Phone to PC 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 4 Challenges r Quality of Service (QoS) Internet provides best of service No guarantee for latency, jitter… r Need Internet connection Home broadband is not reliable r 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 5 Challenges r Security Most unencrypted VOIP spam challenges r Integration into global telephone number system r Emergency call availability & functionality Power, Internet connection Call routing, location service
2: Application Layer 6 QoS r Deal with Jitter Smoothed by playback buffer Will cause more delay in playback Too much delayed packets will be discard (dropped) r Bandwidth 64 kbps or less Depends on codec and use of silence suppression
2: Application Layer 7 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic Mail SMTP, POP3, IMAP r 2.5 DNS r 2.6 P2P file sharing r VOIP r 2.7 Socket programming with TCP r 2.8 Socket programming with UDP
2: Application Layer 8 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API: unreliable datagram (UDP) reliable, byte stream-oriented (TCP) Goal: learn how to build client/server application that communicate using sockets
Application Layer2-9 Socket programming goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end-end- transport protocol Internet controlled by OS controlled by app developer transport application physical link network process transport application physical link network process socket
2: Application Layer 10 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 allows server to talk with multiple clients source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint
TCP Socket Programming Using Python r Python is simpler than C to learn r The 6-th edition textbook uses Python to illustrate the example r Our Unix server support Python and Java programming r Python is a scripting language No need to compile r Python tutorial 2: Application Layer 11
Application Layer2- 12 Client/server socket interaction: TCP wait for incoming connection request connectionSocket = serverSocket.accept() create socket, port= x, for incoming request: serverSocket = socket() create socket, connect to hostid, port= x clientSocket = socket() server (running on hostid ) client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup close connectionSocket read reply from clientSocket close clientSocket
Application Layer2- 13 Example app: TCP client from socket import * serverName = ’servername’ serverPort = clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() Python TCPClient create TCP socket for server, remote port No need to attach server name, port
Application Layer2- 14 Example app: TCP server from socket import * serverPort = serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() Python TCPServer create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket)
2: Application Layer 15 Many Versions of Socket APIs r Unix socket (berkeley socket) r Winsock r MacTCP r …. r We introduce Unix socket API here Can program under SUN OS, Linux, etc A good tutorial on socket programming:
2: Application Layer 16 Socket Descriptor Data Structure Descriptor Table Family: AF_INET Service: SOCK_STREAM Local IP: Remote IP: Local Port: 2249 Remote Port: 3726 Family: AF_INET Service: SOCK_STREAM Local IP: Remote IP: Local Port: 2249 Remote Port: 3726
2: Application Layer 17 TCP Client/Server Socket Overview socket() bind() listen() accept() send() recv() close() socket() TCP Client connect() send() recv() close() connection establishment data request data reply end-of-file notification TCP Server
C Programming Header Files #include r Enough for our socket programming in our Unix server, maybe not enough for other computers 2: Application Layer 18
2: Application Layer 19 What is a Socket? r 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 ” ); 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 int sockfd; /* socket descriptor */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) } perror(“socket”); exit(1); }
2: Application Layer 20 Socket Structure (Client) 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 }; AF_INET // 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 }; … … Big-Endian (Network Byte Order) A 2D 3C 4B IP: 1A.2D.3C.4B 100
2: Application Layer 21 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 bind(sockfd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr)); If you do not designate a specific client port, then no need to use the ‘Bind’ API in client code
2: Application Layer 22 Remote Host Structure struct hostent *hp; hp = gethostbyname(“ “ ” struct hostent { char *h_name; /* official name */ char **h_aliases; /* alias list */ 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 */ feather.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), '\0', 8); // zero the rest Remote host info
2: Application Layer 23 Connect(), send(), recv() by Client connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr); Struct sockaddr sockaddr_in After connecting to the remote sever…. char sendStr[100], recvStr[100]; …. numByteSend=send(sockfd, sendStr, sendByteNum), 0); … recvNumByte = recv(sockfd, recvStr, MaxDataSize, 0); close(sockfd); Blocking call Remote host info Local host socket
Complete Client Code Outline int sockfd; struct sockaddr_in local_addr, remote_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); Assign remote_addr variable (Page 22) connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr); char sendStr[100], recvStr[100]; …. numByteSend=send(sockfd, sendStr, sendByteNum), 0); … recvNumByte = recv(sockfd, recvStr, MaxDataSize, 0); close(sockfd); 2: Application Layer 24
2: Application Layer 25 Partial Send() and recv() Due to multiple packets in transmission #include 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 26 Socket Programming in Server r Still need to initialize local_addr struct (page 21) r No need to initialize remote_addr struct (page 22) r No need to connect() a remote host Instead, need to listen() on specified port (server port) r Accept() a connection request Generate a new socket for one connection Support multiple connections
Complete Server Code Outline 2: Application Layer 27 int sockfd, new_fd; struct sockaddr_in local_addr, remote_addr; assign local_addr (see Page 21 local host part. Change “local_addr.sin_port = 0;” to “local_addr.sin_port = htons(serverPort);” ) 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)) char sendStr[100], recvStr[100]; …. recvNumByte = recv(new_fd, recvStr, MaxDataSize, 0); ….. numByteSend=send(new_fd, sendStr, sendByteNum), 0); … close(new_fd); close(sockfd); int sockfd, new_fd; struct sockaddr_in local_addr, remote_addr; assign local_addr (see Page 21 local host part. Change “local_addr.sin_port = 0;” to “local_addr.sin_port = htons(serverPort);” ) 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)) char sendStr[100], recvStr[100]; …. recvNumByte = recv(new_fd, recvStr, MaxDataSize, 0); ….. numByteSend=send(new_fd, sendStr, sendByteNum), 0); … close(new_fd); close(sockfd); New socket discriptor Following commun. through this
2: Application Layer 28 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 %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 send(new_fd, "Hello, world!\n", 14, 0); ……… close(new_fd); exit(0); } close(new_fd); // parent doesn't need this …………. } child parent
2: Application Layer 29 Fork() r Tuotrial on fork(): r 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. r 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 30 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 SMTP, POP3, IMAP r 2.5 DNS r 2.6 P2P 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 31 Socket programming with UDP 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 UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server
2: Application Layer 32 UDP Socket Programming r sockfd = socket(AF_INET, SOCK_DGRAM, 0) r No connect(), accept() r Send() sendto(), recv() recvfrom() Sendto() includes target address/port SOCK_STREAM (tcp)
2: Application Layer 33 Chapter 2: Summary r Application architectures client-server P2P hybrid r application service requirements: reliability, bandwidth, delay r Internet transport service model connection-oriented, reliable: TCP unreliable, datagrams: UDP Our study of network apps now complete! r specific protocols: HTTP FTP SMTP, POP, IMAP DNS r Some applications Web DNS Internet gaming, VOIP P2P r socket programming
2: Application Layer 34 Chapter 2: Summary r typical request/reply message exchange: client requests info or service server responds with data, status code r message formats: headers: fields giving info about data data: info being communicated Most importantly: learned about protocols r control vs. data msgs in-band, out-of-band (ftp) r centralized vs. decentralized r stateless vs. stateful r reliable vs. unreliable msg transfer r “complexity at network edge”