LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2006, 12:29 AM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
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);   
}
 
Old 03-16-2006, 05:24 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Server Communication question Thor31x Linux - Networking 4 09-27-2005 07:22 AM
Flash Communication server kaboom Linux - Software 0 12-10-2003 02:44 AM
communication accross a proxy server sureshk Linux - Networking 1 10-12-2003 05:37 AM
communication across a proxy server sureshk Linux - Networking 0 10-09-2003 01:12 AM
Live Communication Server? hotrodowner Linux - Software 0 10-07-2003 08:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration