Thread: HTTP Packet Genration and Send/Reciev Via C Socket

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14

    HTTP Packet Genration and Send/Reciev Via C Socket

    I have Build code for sending/receiving TCP Traffic over c Socket .
    Now I want t add HTTP Protocol to it .
    After searching out on google ,one method I found which is by GET method to send a query to a server like google.com and request a page .

    But ,Suppose I saved a HTML page in my directory how can I send HTTP packet via client/server in socket using this HTML page .

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You want to send an HTML page to google.com? Or you want to make an HTTP server/client program?

    I'm assuming the latter since the former makes no sense (unless you're talking about a PUT request).

    If you want to fully implement a simple web server and http client then you will need to read the RFC for the HTTP protocol. Most likey you would want to start with a "toy" version of the protocol implementing only 1 or 2 commands (namely GET and POST).

    Your server would then listen for connections, when a connection came in it would then wait for the client to send something. If the client sends a GET request, you would then want to parse the requested page and transmit that whole HTML page to the client, then close the connection (or keep it alive, possibly...), POST is a little more complex because there are 2 stages where you get input from the client. The client program would then simply connect to your server and send the GET/POST requests

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14
    Server Side Code
    Code:
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    listen(sockfd,5);
     newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr,&clilen);
      while (1)
    {
    
    //Need Help Here ,What Should I code here
    
    }
    Client Side

    Code:
          sockfd = socket(AF_INET, SOCK_STREAM, 0);
        connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        get = build_get_query(host, page);
        fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get);
          while (1)
        {
    
        //Need Help Here ,What Should I code here
    
        }
    
    
    //This code reference from some other program which I need to integarte with mine code
    char *build_get_query(char *host, char *page)
        {
          char *query;
          char *getpage = page;
          char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
          if(getpage[0] == '/'){
            getpage = getpage + 1;
            fprintf(stderr,"Removing leading \"/\", converting %s to %s\n", page, getpage);
          }
          // -5 is to consider the %s %s %s in tpl and the ending \0
          query = (char *)malloc(strlen(host)+strlen(getpage)+strlen(USERAGENT)+strlen(tpl)-5);
          sprintf(query, tpl, getpage, host, USERAGENT);
          return query;
        }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,682
    If you're still at the stage of "what goes here", then you first need to learn some of the basics of network programming.
    Beej's Guide to Network Programming

    Once you've mastered how to send/receive from a remote host, then you can work on the 'how to talk http' with that remote.
    For this, you'll need RFC's.
    RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
    through to
    RFC 7235 - Hypertext Transfer Protocol (HTTP/1.1): Authentication

    But that's an awful lot of work.
    Perhaps a library to do all the low-level stuff for you is more what you want.
    libcurl - the multiprotocol file transfer library
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Libcurl is great to play around with !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send HTTP with sockets.
    By Orr Goren in forum C Programming
    Replies: 2
    Last Post: 04-09-2013, 09:45 AM
  2. How to send a FIN with my last packet???
    By klipseracer in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2008, 12:40 AM
  3. Send()s being clumped into one packet
    By Yasir_Malik in forum Windows Programming
    Replies: 4
    Last Post: 05-03-2006, 09:58 PM
  4. Preparing packet to send
    By davide_82 in forum C Programming
    Replies: 3
    Last Post: 11-05-2005, 02:28 PM
  5. Packet Filter using Unix Socket
    By doraiashok in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-12-2003, 08:14 AM

Tags for this Thread