16-Network_Programming_II_2spp

16-Network_Programming_II_2spp - CSC 4304 - Systems...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 4304 - Systems Programming Fall 2010 Tevfik Ko ! ar Louisiana State University November 11th, 2010 Lecture - XVI Network Programming - II Simple Web Server 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Logic of a Web Server • 1. Setup the server socket, bind, listen • 2. Accept a connection accept, fdopen • 3. Read a request fread • 4. Handle the request a. directory --> list it : opendir, readdir b. regular file --> cat the file : open, read c. .cgi file --> run it : exec d. not exist --> error message • 5. Send a reply fwrite 3 1. Setup the Server int init_socket(int portnum) { ... gethostname( hostname , 256 ); /* where am I ? */ hp = gethostbyname( hostname ); /* get info about host */ ... bzero( (void *)&saddr, sizeof(saddr) ); bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length); saddr.sin_family = AF_INET ; /* fill in socket type */ saddr.sin_port = htons(portnum); /* fill in socket port */ sock_id = socket ( AF_INET, SOCK_STREAM, 0 ); /* get a socket */ ... rv = setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); ... bind (sock_id, (struct sockaddr *) &saddr, sizeof(saddr)); ... listen (sock_id, 1) != 0 ); ... return sock_id; } 4
Background image of page 2
int main(int ac, char *av[]) { ... sock = init_socket(portnum); ... /* main loop here */ while(1){ /* take a call and buffer it */ fd = accept ( sock, NULL, NULL ); ... fpin = fdopen (fd, "r" ); fpout = fdopen (fd, "w" ); /* read request */ fgets (request,BUFSIZ,fpin); ...
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/23/2011 for the course CSC 4303 taught by Professor Staff during the Fall '10 term at LSU.

Page1 / 12

16-Network_Programming_II_2spp - CSC 4304 - Systems...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online