Thread: HTTP Web Server and GET Request

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Question HTTP Web Server and GET Request

    Hi, I am new around here but was hoping I may receive some guidance. I have limited experience with C as well as network programming but nevertheless I have a project in which I am trying to simulate a browser requesting a video file. Knowing that creating an HTTP server might be pretty involved so I used some sample code namely the Tiny Web server code (tiny.tar) from CS:APP2e, Bryant and O'Hallaron. When I tried running said code on an unix server. i get the following

    Open_listenfd error: Permission denied
    This is what the function open_listenfd does:

    Code:
    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;
    
        /* 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;
    }
    /* $end open_listenfd */
    So my understanding is that open_listenfd merely opens/create a listening socket. Please let me know what might be preventing me from doing so (do I need to create my own listen socket code or if I need to use a specific port?). I just want to see how a HTTP server works and then determine how to create an HTTP GET request for a video file. Also please feel free to direct me to any resources that might help me accomplishing this endeavor.

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Ports < 1024 are privileged, so if you're listening on one of those, you must start the application as a privileged user, like root.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    Ports < 1024 are privileged, so if you're listening on one of those, you must start the application as a privileged user, like root.
    Ah thanks for pointing that out. I will use ports > 1024 then. Getting past the error, I tried running the code and confirmed through telnet that I am able to establish connection. Now I am trying to see how I can create an HTTP GET request in C. Now to request a file is it simply creating and sending a string similar to below to request for the file on the server?

    GET /filename HTTP/1.0

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Quote Originally Posted by ytakeru12 View Post
    Also please feel free to direct me to any resources that might help me accomplishing this endeavor.
    Beej's Guide to Network Programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting body of HTTP request
    By c_weed in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2011, 10:21 AM
  2. Sending http get request
    By LeeVerr in forum C Programming
    Replies: 4
    Last Post: 09-07-2010, 09:02 PM
  3. HTTP GET request
    By aosmith in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 07:00 AM
  4. HTTP GET and POST REQUEST
    By strickey in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-20-2007, 04:23 PM

Tags for this Thread