Introduction to Project 1Project GoalsHTTP UsageRequest - ResponseHTTP requestFour partsWhy do we need sockets?SocketFunctionsTypes of SocketsAddressingClient – high level viewSlide 13Server – high level viewSlide 15accepting a client connection (TCP)Sending / Receiving DataSlide 18Dealing with blocking callsDealing with blocking (cont..)select function callQuestions?Introduction to Project 1Web Client and ServerJan 2006Project GoalsImplement a simple WWW client and serverImplement a tiny subset of HTTPTo give you experience with HTTP and sockets programmingTo be familiar with Unix, Minet and prepare for the subsequent part of the projectHTTP UsageHTTP is the protocol that supports communication between web browsers and web servers.Most clients/servers today use version 1.1, but 1.0 is also in use.HTTP protocol works on top of TCP.telnet www.cs.northwestern.edu 80 telnet www.ece.northwestern.edu 80Request - ResponseHTTP has a simple structure:client sends a requestserver returns a replyHTTP requestRequest lineMethod URL HTTP-Version\r\nHeader linesContentRequest-LineHeaders...Content...blank lineblank lineFour parts0: Get build, configure and run the Minet stack1: HTTP Client2: Connection-at-a-time HTTP Server3: Simple select-based Multiple-connection-at-a-time server4: Complex Select-based Multiple-connection-at-a-time Serve ( Extra Credit )Why do we need sockets?Provides an abstraction for interprocess communicationSocketThe services (often provided by the operating system) provide the interface between application and protocol software.ApplicationApplicationNetwork APINetwork APIProtocol AProtocol AProtocol BProtocol BProtocol CProtocol CFunctionsDefine an “end- point” for communicationInitiate and accept a connectionSend and receive dataTerminate a connection gracefullyExamples File transfer apps (FTP), Web browsers(HTTP), Email (SMTP/ POP3), etc…Types of SocketsTwo different types of sockets :stream vs. datagramStream socket :( a. k. a. connection- oriented socket)It provides reliable, connected networking serviceError free; no out- of- order packets (uses TCP)applications: telnet/ ssh, http, …Datagram socket :( a. k. a. connectionless socket)It provides unreliable, best- effort networking servicePackets may be lost; may arrive out of order (uses UDP)applications: streaming audio/ video (realplayer), …AddressingClientServerClient – high level viewCreate a socketSetup the server addressConnect to the serverRead/write dataShutdown connectionint connect_ socket( char *hostname, int port) {int sock;struct sockaddr_in sin;struct hostent *host;sock = socket( AF_ INET, SOCK_ STREAM, 0);if (sock == -1)return sock;host = gethostbyname( hostname);if (host == NULL) {close( sock);return -1;}memset (& sin, 0, sizeof( sin));sin. sin_ family = AF_ INET;sin. sin_ port = htons( port);sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0];if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) {close (sock);return -1;}return sock;}Resolve the hoststruct hostent *gethostbyname( const char *hostname);/*Return nonnull pointer if OK, NULL on error */Setup up the structunit16_t htons(unit16_t host16bitvaule)/*Change the port number from host byte order to network byte order */Connectconnect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen)/*Perform the TCP three way handshaking*/Hostent structurestruct hostent{ char * h_name /*official name of host*/ char ** h_aliases; /* pointer of array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length /* length of address */ char ** h_addr_list /*prt to array of ptrs with \ IPv4 or IPv6 address*/}Ipv4 socket address structurestruct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr /* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/}RMake the socketSocket(int family , int type, in t protocol); return nonnegative value for OK, -1 for errorServer – high level viewCreate a socketBind the socketListen for connectionsAccept new client connectionsRead/write to client connectionsShutdown connectionint make_ listen_ socket( int port) {struct sockaddr_ in sin;int sock;sock = socket( AF_ INET, SOCK_ STREAM, 0);if (sock < 0)return -1;memset(& sin, 0, sizeof( sin));sin. sin_ family = AF_ INET;sin. sin_ addr. s_ addr = htonl( INADDR_ ANY);sin. sin_ port = htons( port);if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0)return -1;return sock;}Make the socketSetup up the structBindbind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen);/* return 0 if OK, -1 on errorassigns a local protocol adress to a socket*/Listening on a port (TCP)accepting a client connection (TCP)int get_ client_ socket( int listen_ socket) {struct sockaddr_ in sin;int sock;int sin_ len;memset(& sin, 0, sizeof( sin));sin_ len = sizeof( sin);sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len);return sock;}Setup up the structAccept the client connectionaccept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen)/* return nonnegative descriptor if OK, -1 on error return the next completed connection from the front of the completed connection queue. if the queue is empty, the process is put to sleep(assuming blocking socket)*/Sending / Receiving DataWith a connection (SOCK_STREAM):int count = write(sock, &buf, len);count: # bytes transmitted (-1 if error)buf: char[], buffer to be transmittedlen: integer, length of buffer (in bytes) to transmitint count = read(sock, &buf, len);count: # bytes received (-1 if error)buf: void[], stores received byteslen: # bytes receivedCalls are blocking [returns only after data is sent (to socket buf) / received]socket()bind()listen()accept()read()write()read()close()Socket()connect()write()read()close()TCP ClientTCP ServerWell-known portblocks until connection from clientprocess requestConnection establishmentData(request)Data(reply)End-of-file notificationDealing with blocking callsMany functions block accept(), connect(), All read() and write()For simple programs this is fineWhat about complex connection routinesMultiple connectionsSimultaneous sends and
View Full Document