开发者

how to display sample text in socket programming c++

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.s开发者_如何转开发in_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; 
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜