15-213 Recitation 12: 11/25/02 Outline Socket Interface –Echo Client/Server Http Protocol Evaluation Annie Luo Office Hours: Thursday.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Network Programming Topics Peeking at Internet traffic Programmer’s view of the Internet (review) Sockets interface Writing clients and servers Understanding.
Chris Riesbeck, Fall 2007 Network Programming Topics –Sockets interface –Writing clients and servers.
Socket Programming Application Programming Interface.
Carnegie Mellon 1 Network Programming : Introduction to Computer Systems 20 th Lecture, Nov. 2, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Network Programming Topics Sockets interface Writing clients and servers CS 105 “Tour of the Black Holes of Computing!”
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Networks and Network Programming May 24, 2006 Topics Client-server programming model Networks A programmer’s view of the Internet Sockets interface Writing.
– 1 – CS213, S’08 Network Programming June 4, 2008 Topics Sockets interface Writing clients and servers.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
Network Programming November 19, 2007 Topics Peeking at Internet traffic Programmer’s view of the Internet (review) Sockets interface Writing clients and.
Network Programming Nov. 6, 2008 Topics Peeking at Internet traffic Programmer’s view of the Internet (review) Sockets interface Writing clients and servers.
Network Programming Nov 21, 2002 Topics Programmer’s view of the Internet (review) Sockets interface Writing clients and servers class26.ppt “The.
Introduction to Project 1 Web Client and Server Jan 2006.
Internetworking II: Network programming April 20, 2000 Topics client/server model Berkeley sockets –TCP client and server examples –UDP client and server.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Networking S04, Recitation, Section A
1. Networks and the Internet 2. Network programming.
Networking Alan L. Cox T. S. Eugene Ng Some slides adapted from CMU slides.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
University of Amsterdam Computer Systems – Iterative server Arnoud Visser 1 Computer Systems Iterative server.
Computer Systems II CSC 2405 Network Programming.
INTERNET L4: Internet. Last Lecture Re-cap: IP Internet  Based on the TCP/IP protocol family  IP (Internet protocol) : Provides basic naming scheme.
Network Programming Topics Programmer’s view of the Internet Sockets interface Writing clients and servers CS 105 “Tour of the Black Holes of Computing!”
Recitation 12 (Nov. 29) Outline Socket programming Lab 7: part 1 Reminder Lab 7: Due next Thursday Minglong Shao Office hours: Thursdays.
Carnegie Mellon 1 Network Programming / : Introduction to Computer Systems 21 st Lecture, Nov. 7, 2013 Instructors: Randy Bryant, Dave O’Hallaron,
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Network Programming September 6, 2005 Topics Programmer’s view of the Internet Sockets interface Writing clients and servers Concurrency with I/O multiplexing.
Network programming Nov 16, 2000 Topics Client-server model Sockets interface Echo client and server class24.ppt “The course that gives CMU its.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Nov. 5 th Instructors: Roger Dannenberg and Greg Ganger.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Carnegie Mellon 1 Network Programming / : Introduction to Computer Systems 21 st Lecture, Nov. 8, 2012 Instructors: Dave O’Hallaron, Greg.
Network Programming Topics Sockets interface Writing clients and servers CS 105 “Tour of the Black Holes of Computing!”
– 1 – , Spring 2006 Network Programming Introduction Jan. 25, 2006 Topics Programmer's view of the Internet Sockets interface Writing clients and.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
Carnegie Mellon Introduction to Computer Systems /18-243, spring rd Lecture, Apr. 14 th Instructors: Gregory Kesden and Markus Püschel.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~
1 Networking Programming (I). A Client-Server Transaction Client process Server process 1. Client sends request 2. Server handles request 3. Server sends.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 Networking Programming. 2 Outline Connection & Socket Sockets Interface –Functions –Echo Client and Server Suggested Reading: –11.4.
Networking Alan L. Cox Some slides adapted from CMU slides.
Carnegie Mellon 1 Network Programming / : Introduction to Computer Systems 21 st Lecture, April 2, 2015 Instructors: Franz Franchetti, Seth.
– 1 – , Fall 2003 Network Programming Introduction Sept. 1, 2004 Topics Programmer's view of the Internet Sockets interface Writing clients and servers.
Extending echo server HTTP Broken pipe error Feedback and evaluation
Instructors: Anthony Rowe, Seth Goldstein and Greg Kesden
Network Programming /18-243: Introduction to Computer Systems
CS 105 “Tour of the Black Holes of Computing!”
Network Programming April 11, 2008
Network Programming Jan 13, 2005
Internet Topics Client-server programming model Networks in general
Network programming Nov 27, 2001
Recitation 11 – 4/29/01 Outline Sockets Interface
Web Services Topics Proxies Sockets interface
Network Programming Chapter 12
Networking Programming
Network Programming: Part II CSCI 380: Operating Systems Lecture #13
Networking Programming
Network Programming: Part I CSCI 380: Operating Systems
Network Programming: Part II CSCI 380: Operating Systems
Network Programming November 3, 2008
Internet Networking recitation #8
Presentation transcript:

Recitation 12: 11/25/02 Outline Socket Interface –Echo Client/Server Http Protocol Evaluation Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402 What’s Left L7 Proxy Due: Thursday, Dec. 5 Final: Tuesday, Dec. 17, 8:30am, Porter Hall 100

Client-Server Programming Model Client and server communicate over connections –point-to-point, full-duplex, and reliable Connection uniquely identified by socket pair –cliaddr:cliport, servaddr:servport Port: 16-bit integer that identifies a process: –ephemeral port: assigned automatically on client when client makes a connection request –well-known port: associated with some service provided by a server Port 7: echo server Port 23: telnet server Port 25: mail server Port 80: web server

Key Data Structure Defined in /usr/include/netinet/in.h Must cast ( sockaddr_in * ) to ( sockaddr * ) for connect, bind, and accept /* generic socket address */ struct sockaddr { unsigned short sa_family; /* protocol family */ char sa_data[14]; /* address data */ }; /* internet specific socket address */ struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port number (big-endian) */ struct in_addr sin_addr; /* IP address (big-endian) */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ };

More on Socket Socket: special descriptor for networking I/O –Unix uses the same abstraction for file I/O and network I/O –client and server communicate by reading/writing to/from socket descriptors –use robust I/O functions Socket I/O differs from file I/O –more complicated when application “opens” socket descriptors

Socket Interface Client Server socket bind listen accept rio_readlineb rio_writen close rio_readlineb connect rio_writen close Connection request EOF Await connection request from next client open_listenfd open_clientfd

Echo Client Main Routine #include “csapp.h” /* usage:./echoclient host port */ int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } Close(clientfd); exit(0); }

Echo Client: open_clientfd() int open_clientfd(char *hostname, int port) { int clientfd; struct hostent *hp; struct sockaddr_in serveraddr; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */ /* Fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); /* Establish a connection with the server */ if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd; }

Echo Client: open_clientfd() (socket) socket() creates a socket descriptor on the client –AF_INET indicates that the socket is associated with Internet protocols –SOCK_STREAM : selects a reliable byte stream connection int clientfd; /* socket descriptor */ … if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */

Echo Client: open_clientfd() (gethostbyname) Then the client builds the server’s Internet address. int clientfd; /* socket descriptor */ struct hostent *hp; /* DNS host entry */ struct sockaddr_in serveraddr; /* server’s IP address */ /* Fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port);

Echo Client: open_clientfd() (connect) Finally the client creates a connection with the server –client process suspends (blocks) until the connection is created with the server –after resuming, the client is ready to exchange messages with the server via Unix I/O calls on the descriptor sockfd int clientfd; /* socket descriptor */ struct hostent *hp; /* DNS host entry */ struct sockaddr_in serveraddr; /* server’s IP address */ /* Establish a connection with the server */ if (connect(clientfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd;

Echo Server Main Routine int main(int argc, char **argv) { int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp; port = atoi(argv[1]); /* the server listens on a port passed on the command line */ listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("server connected to %s (%s)\n", hp->h_name, haddrp); echo(connfd); Close(connfd); }

Echo Server: open_listenfd() int open_listenfd(int port) { int listenfd, optval = 1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int)) < 0) return -1;... (more)

Echo Server: open_listenfd() (cont)... /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1; /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; return listenfd; }

socket() creates a socket descriptor on the server –AF_INET : indicates that the socket is associated with Internet protocols. –SOCK_STREAM : selects a reliable byte stream connection. Echo Server: open_listenfd() (socket) int listenfd; /* listening socket descriptor */ /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1;

Echo server: open_listenfd() (setsockopt) Give the socket some attributes Allow to rerun the server immediately after we kill it –otherwise we would have to wait about 15 secs –eliminates “Address already in use” error from bind() –strongly suggest you do this for all your servers to simplify debugging /* Eliminates "Address already in use" error from bind */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int)) < 0) return -1;

Echo Server: open_listenfd() (initialize socket address) Next initialize the socket with the server’s Internet address (IP address and port) IP addr and port stored in network (big-endian) byte order –htonl() converts longs from host byte order to network byte order –htons() convers shorts from host byte order to network byte order struct sockaddr_in serveraddr; /* server’s socket addr */... /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port);

Echo Server: open_listenfd() (bind) bind() associates the socket with the socket address we just created. int listenfd, /* listening socket */ struct sockaddr_in serveraddr; /* server’s socket addr */... /* listenfd will be an endpoint for all requests to port on any IP address for this host */ if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1;

Echo server: open_listenfd (listen) listen() indicates that this socket will accept connection ( connect() ) requests from clients Finally ready to enter the main server loop that accepts and processes client connection requests int listenfd, /* listening socket */... /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; return listenfd; }

Echo Server: Main Loop The server loops endlessly, waiting for connection requests, then reading input from the client, and echoing the input back to the client. main() { /* create and configure the listening socket */ while(1) { /* Accept(): wait for a connection request */ /* echo(): read and echo input lines from client til EOF */ /* Close(): close the connection */ }

accept() blocks waiting for a connection request accept() returns a connected descriptor ( connfd ) with the same properties as the listening descriptor ( listenfd ) –returns when connection between client and server is complete –all I/O with the client will be done via the connected socket accept() also fills in client’s address Echo Server: accept() int listenfd; /* listening descriptor */ int connfd; /* connected descriptor */ struct sockaddr_in clientaddr; int clientlen; clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

accept() Illustrated listenfd(3) client 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd. clientfd server listenfd(3) client clientfd server 2. Client makes connection request by calling and blocking in connect. listenfd(3) client clientfd server 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd. connection request connfd(4)

Echo Server: Identifying the Client The server can determine the domain name and IP address of the client struct hostent *hp; /* pointer to DNS host entry */ char *haddrp; /* pointer to dotted decimal string */ hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("server connected to %s (%s)\n", hp->h_name, haddrp);

Echo Server: echo() The server uses RIO to read and echo text lines until EOF is encountered –EOF notification caused by client calling close(clientfd) void echo(int connfd) { size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { printf("server received %d bytes\n", n); Rio_writen(connfd, buf, n); }

Web Servers Clients and servers communicate using the HyperText Transfer Protocol (HTTP) –Client and server establish TCP connection –Client requests content –Server responds with requested content –Client and server close connection (usually) Current version is HTTP/1.1 Web server HTTP request HTTP response (content) Web client (browser)

HTTP Protocol Anatomy unix> telnet 80 Client: open connection to server Trying Telnet prints 3 lines to the terminal Connected to aol.com. Escape character is '^]'. GET / HTTP/1.1 Client: request line host: Client: required HTTP/1.1 HOST header Client: empty line terminates headers. HTTP/ OK Server: response line MIME-Version: 1.0 Server: followed by five response headers Date: Mon, 08 Jan :59:42 GMT Server: NaviServer/2.0 AOLserver/2.3.3 Content-Type: text/html Server: expect HTML in the response body Content-Length: Server: expect 42,092 bytes in the resp body Server: empty line (“ \r\n ”) terminates hdrs Server: first HTML line in response body... Server: 766 lines of HTML not shown. Server: last HTML line in response body Connection closed by foreign host. Server: closes connection unix> Client: closes connection and terminates