Hi,

I am writing a program that is to behave as a web server. Both the client and the server code are on my laptop. Ideally, I should be able to type in 127.0.0.1: PORT/index.html in Firefox and the server code should reply appropriately. So far my browser and the server are talking - that is, Firefox sends the request packet that I am printing on the screen.

I am having trouble with figuring out how to send a response to the client. How does one develop a response packet? Do I just write the data into a buffer array of some sort and then push that through? How do I embed/include the index.html page in the response packet?

Any help would be greatly appreciated.

Thanks!

Recommended Answers

All 4 Replies

Check out this. This already has some sample code with it

this for a more conceptual explanation

Thanks for that. The Server Code is helpful. But just a question, I have often noticed Child and Parent processes in server codes. Is there any benefit to using a child process with fork()? Can I not just do
if(send(new_fd, "This is a test string from server!\n", 37, 0) == -1) on the same new socket that accept() returned? Im not sure I understand the need for child processes.

Also, the above code is just sending a test string. I want to send the whole Http response packet..so something along the lines of:
HTTP/1.1 200 OK
Connection: close
Date: 9/9/2010
Data: (index.html)

If I were to do that would I just write the above into a string array and then use the send() function? Plus how will the index.html page be sent? =s

Is there any benefit to using a child process with fork()?

Definitely so. Doing everything in the same process means that you can either accept or serve. The act of serving (read the request, prepare, and send the reply) is time consuming. That is, server becomes unresponsive.
NB: it is possible to make a good server in a single process, but it is way more complicated.

If I were to do that would I just write the above into a string array and then use the send() function?

Yes.

Plus how will the index.html page be sent?

use the send() function. Don't forget an empty line after the headers.

Thanks. That helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.