120 likes | 220 Views
2.1 Principles of app layer protocols 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail 2.5 DNS. 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching Content distribution networks P2P file sharing.
E N D
2.1 Principles of app layer protocols 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail 2.5 DNS 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching Content distribution networks P2P file sharing Chapter 2 outline 2: Application Layer
An application-created, OS-controlled interface (a “door”) into which an application process can both send and receive messages to/from another application process socket Socket programming Goal: learn how to build client/server applications 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 • reliable, byte stream-oriented 2: Application Layer
process process TCP with buffers, variables TCP with buffers, variables socket socket Socket programming using TCP Socket: a door between an application process and end-to-end transport protocol (UDP or TCP) TCP service: reliable transfer of bytesfrom one process to another controlled by application developer controlled by application developer controlled by operating system controlled by operating system Internet host or server host or server 2: Application Layer
Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact - listen Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process trying to connect: 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)? TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint Socket programming with TCP 2: Application Layer
create socket, connect to “hostname”, port=x create socket, port=x, for incoming request: socket(), connect() socket(), bind(), listen() TCP connection setup wait for incoming connection request accept() send request write() read request read() write reply write() read reply from read() close close() close close() Client/server socket interaction: TCP Server (running on “hostname”) Client 2: Application Layer
Berkeley Socket API for TCP: Server Client int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *myaddr, int addrlen); int listen(int sockfd, int backlog); int socket(…); int connect(int sockfd, struct sockaddr *servaddr, int addrlen); int accept(int sockfd, struct sockaddr *peer, int *addrlen); int send(int sockfd, char *buff, int nbytes, int flags); int recv(int sockfd, char *buff, int nbytes, int flags); int send(…); int recv(…); int close(int fd); int close(…); 2: Application Layer
Sketch of a TCP Server if (( sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror(“server: can’t open stream socket”); bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) perror(“server: can’t bind local address”); listen(sockfd, 5); while(1){ newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); /* receive request from client, send reply */ close(newsockfd); } 2: Application Layer
Sketch of a TCP Client bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(SERVER_HOST_IP_ADDRESS); serv_addr.sin_port = htons(SERVER_TCP_PORT); if (( sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror(“client: can’t open stream socket”); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) perror(“client: cannot connect to server”); /* send request to server, receive reply */ close(sockfd); } 2: Application Layer
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 UDP: transmitted data may be received out of order, or lost UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server application viewpoint Socket programming with UDP 2: Application Layer
Client create socket, port=x, for incoming request: socket(), bind() create socket, Create data + (“hostname”, port=x) send datagram request sendto() socket() read request from recvfrom() write reply sendto() specifying client host address, port number read reply recvfrom() close close() Client/server socket interaction: UDP Server (running on “hosname”) 2: Application Layer
Berkeley Socket API for UDP: Server Client int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *myaddr, int addrlen); int socket(…); int bind(…); int sendto(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *from, int *addrlen); int sendto(…); int recvfrom(…); rint close(int fd); int close(…); 2: Application Layer
handles HTTP/1.0 requests accepts a request parses header obtains requested file from server’s file system creates HTTP response message: header lines + file sends response to client Repeat for another request creates a transaction log (you) show statistics (delay, size, elapsed time of different phases, etc). see assignment put some files in your directory (both small/KB and large/MB objects) after creating server, you can request file using a browser (e.g. IE explorer) will be tested by other people. see assignment http://java.sun.com Building a simple Web server 2: Application Layer