-1

I got an program that creates an socket for given port no .. i need to display an hello world text while it is accessed in browser say if the port is 8080 while visiting i need to display an hello world text using c++ prog:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(newsockfd,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockfd);
     return 0; 
}
raja
  • 1
  • 1
  • Sorry, what is your question? – Drahakar Apr 26 '11 at 15:01
  • Please clarify whether you are trying to send sample text to a telnet client or whether you are trying to write an HTTP server that would be accessed by a web browser. – Michael Greene Apr 26 '11 at 15:02
  • yes i am trying to write an http server that would be access by web browser – raja Apr 26 '11 at 15:04
  • Please give more information. What is the problem with the code above? Should the "hello world" appear in the browser or in the console the "server" is runnning in? – AndreasT Apr 26 '11 at 15:05
  • basically use write( fd, &buffer, sendlength ) - question is, as all others pointed out, what should be IN the buffer... – Mario The Spoon Apr 26 '11 at 15:05

3 Answers3

1

Web Browsers talk HTTP when you start a URL with http:// so your server will need to talk HTTP as well.

The Browser will typically send

GET / HTTP/1.1
Host: my_website
Header1: blah blah
Header2: blah blah

The request ends with Carriage return(CR) linefeed(LN), CR LN

You will then need to create your response which will be something like

HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/plain

Hello World

With the blank line all new line having CRLN

As others have mentioned you will probably want to do something to ensure your read does not block your server.

doron
  • 27,972
  • 12
  • 65
  • 103
0

Your code won't do that. Its going to block at the 'read(newsockfd..' until it gets input which the browser isn't going to send.

It looks like you have copied some socket server sample. It would probably be fine with the corresponding client sample but not with a browser.

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
0

If the page in the browser should display "Hello World!" then just write that instead of "I got your message".

Your example works. Compile & run with

g++ socket.cpp
./a.out 8080

Point your browser at it:

firefox http://localhost:8080

The browser should say:I got your message

Note: You got this example from linuxhowtos.org. Pointing people to that might also help them deduce issues.

AndreasT
  • 9,417
  • 11
  • 46
  • 60