16-Network_Programming_II

16-Network_Programming_II - CSC 4304 Systems Programming...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 4304 - Systems Programming Fall 2010 Tevfik Ko ! ar Louisiana State University November 11th, 2010 Lecture - XVI Network Programming - II Simple Web Server 2 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) ); /* zero struct & fill host addr*/ 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 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); ... while( fgets(buf,BUFSIZ,fp) != NULL && strcmp(buf,"\r\n") != 0 ) ; /* do what client asks */ process_rq(request, fpout); ...
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern