LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   server/browser communication (https://www.linuxquestions.org/questions/programming-9/server-browser-communication-425270/)

calorie712 03-16-2006 12:29 AM

server/browser communication
 
1. Your firefox browser will connect to your server with a (generally) three-line HTTP
request. Your server needs to read those three lines.

2. Your server will then send back an HTTP response (followed, presumably, by some HTML).

3. At that point, you can close "newsockfd" (thus closing the TCP/IP connection) and
loop back to "accept()" the next client request.

I understand the abstract above. In the code below, how do I actually send back an HTTP response? Also, what do I do with the HTTP request when I read it in from the Firefox browser (what would the code look like?) Thank you so much for your help, I really want to know how this works.

Code:

/* Library Section */
#include <stdio.h>        /* Standard Input and Output */
#include <unistd.h>      /* Unix Standard Functions */
#include <string.h>      /* Used for bzero */
#include <math.h>        /* Math Library used for log10 */
#include <sys/types.h>    /* Used for System Data Type Defitions */
#include <sys/socket.h>  /* Used for sockets */
#include <netinet/in.h>  /* sockaddr_in definition */
#include <arpa/inet.h>    /* Internet Sockets */
#include <stdlib.h>


/* Definitions */
#define SERV_TCP_PORT    10617            /* Port Number */
#define SERV_HOST_ADDR  "198.102.147.137" /* IP address for Gimli */
#define BUFFSIZE        500              /* Buffer Size */


int main() {
  int    sockfd;      /* Initial socket descriptor */
  int    newsockfd;  /* Client/Server socket descriptor */
  socklen_t client_len;  /* Client Socket File Length */
  struct sockaddr_in serv_addr;  /* Server Address Information */
  struct sockaddr_in cli_addr;    /* Client Address Information */
  char    userstring[BUFFSIZE];    /* string from the user */
  char*  revstring;      /* string reversed from the server */
 

  /* Open a TCP socket (an Internet stream socket). */

  if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)/* creating internet socket */
    fprintf(stderr, "server: can't open stream socket\n");

  /* Bind our local address so that the client can send to us. */
 
  /* First, clear out the structure */
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family      = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port        = htons(SERV_TCP_PORT);

  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    fprintf(stderr, "server: can't bind local address\n");

  /* Now, listen for incomming connections. */
  listen(sockfd, 5);
  printf("The server is now listening for connections\n");
  bzero(userstring, BUFFSIZE);
  while (1) {    /* loop forever */

    client_len = sizeof(cli_addr);
    /* Accept the client connection */
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client_len);
    if (newsockfd < 0)
      fprintf(stderr, "server: accept error");

 
 
/* Write string to the socket */
 
  write(newsockfd, revstring, strlen(revstring));
 
    bzero(userstring, BUFFSIZE);
  } /* END WHILE */
  close(newsockfd);    /* We are finished with the socket */
  close(sockfd);    /* We are finished with the socket */
  return (0); 
}


Mara 03-16-2006 05:24 PM

Well...the request sent by modern browsers is actually much longer than 3 lines. It's important to know that it finishes after two line ends (\r\n\r\n).

Now..after accept() you should read() the request to a buffer. The beginning should look like
GET / HTTP/1.1
You may also get HTTP/1.0 instead of 1.0. / in this example is the file the server is asked (usually the answer will be index.html).

When you know what you're asked for, you need to read until the request ends. Then send your answer. Using simple write() call.


All times are GMT -5. The time now is 06:40 AM.