1

I am trying to make a simple http server with C++. I've followed the beej's guide of network programming in C++.

When I ran the server in some port (8080, 2127, etc.) it successfully send response to browser (Firefox) when it accessed via address bar with: localhost:PORT_NUMBER except in port 80.

This is the code i wrote:

printf("Server: Got connection from %s\n", this->client_ip);

if(!fork()) // This is the child process, fork() -> Copy and run process
{
    close(this->server_socket); // Child doesn't need listener socket

    // Try to send message to client
    char message[] = "\r\nHTTP/1.1 \r\nContent-Type: text/html; charset=ISO-8859-4 \r\n<h1>Hello, client! Welcome to the Virtual Machine Web..</h1>";
    int length = strlen(message); // Plus 1 for  null terminator
    int send_res = send(this->connection, message, length, 0); // Flag = 0

    if(send_res == -1)
    {
        perror("send");
    }

    close(this->connection);

    exit(0);
}

close(this->connection); // Parent doesn't need this;

The problem is, even I have added the header on very early of the response string, why does the browser not showing the HTML properly instead shows only plain text? It shows something like this:

Content-Type: text/html; charset=ISO-8859-4

<h1>Hello, client! Welcome to the Virtual Machine Web..</h1>

Not a big "Hello, client!.." string like a normally h1 tagged string. What is the problem? Am I missing something in the header?

Another question is, why is the server won't running in port 80? The error log in server says:

server: bind: Permission denied
server: bind: Permission denied
Server failed to bind
libc++abi.dylib: terminate called throwing an exception

Please help. Thank you. Edit: I'dont have any process on Port 80.

3
  • re port 80, do you have apache or another web server already listening on that port? Commented Nov 22, 2012 at 1:18
  • No, I don't have any server or proccess using that port. Any ideas? Commented Nov 22, 2012 at 1:20
  • 4
    You need to be root to bind to ports <1024. Commented Nov 22, 2012 at 1:25

3 Answers 3

3

You need to terminate the HTTP response header with \r\n\r\n, rather than just \r\n. It should also start with something more like HTTP/1.1 200 OK\r\n, without the leading \r\n.

For your port problem, if you have nothing else running on the port in question, you may find that the socket created by the last run of your program is still sticking around. To work around this, you can use setsockopt to set the SO_REUSEADDR flag on the socket. (This is not recommended for general use, I believe because you may receive data not intended for your program, but for development it's extremely handy.)

Sign up to request clarification or add additional context in comments.

1 Comment

My program never use port 80 because I don't know how to use port 80. Also, no other app bound to this port. I believe port issue is because privilege problem. However, your answer about HTTP response header is correct. Thank you.
3

Your request starts with \r\n when it shouldn't also it did not specify a status code and you need a blank line after all the headers.

char message[] = "HTTP/1.1 200 Okay\r\nContent-Type: text/html; charset=ISO-8859-4 \r\n\r\n<h1>Hello, client! Welcome to the Virtual Machine Web..</h1>";

As for your port 80 issue, some other application maybe bound to it.

Comments

0

you need to add "Content-length: ", and the length is your HTML code, just like this:

char msg[] = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-length: 20\r\n\r\n<h1>Hello World</h1>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.