Unix C++ simple server question: sending data back to browser
I am creating a simple Unix server written in C++ that simply waits for incoming connections, and then when a connection is established, it sends the requested data back to the client's browser to be displayed. I have everything working except for the sending of the data to the client. This is how it should work:
I start server and set port number (for this开发者_JS百科 example i'll use 8181).
Client opens browser and connects to my server located at http://server.mysite.com:8181/test.txt and attempts to retrieve test.txt for viewing.
Server accepts incoming connection. It checks to see if file exists. If file exists, it sends the file to the browser to be viewed.
My problem is that I'm not sure how to send the data back to the browser to be viewed. I'm trying to use Unix system calls to get this accomplished, as I believe that's all that should be needed to get the data back.
Right now all I'm doing is read()ing in from the file into a char array buffer and then feeding that into the write(socketID, buffer, strlen(buffer)) command. When I test it, the server correctly finds the file and reads the data into the char array buffer, but the write() command is returning a -1 error value every time.
I would like to get it to where the browser connects and then is able to view the text.txt in the browser as well as browser-supported picture formats.
Is there some way to workaround the "reading in to a buffer then writing that buffer to a socket" part and just somehow send the file directly to the browser? Here is part of the code I'm working with now. I just commented in other parts of code.
//create and bind socket. call listen()...
conn_socket = accept(socketInt, (struct sockaddr *)&caddr, (socklen_t *) &caddrLength);
recvInt = recv(conn_socket, requestedFileBuffer, 1023, 0);
if(recvInt > 0){
//checks to see that the requestd file exists and correctly
//reads that file into char array "buffer"
int writeInt = write(recvInt, buffer, strlen(buffer));//This is not working
cout << "writeInt is: " << writeInt << endl;// returns -1
}//end if
Anyone have any suggestions? If you need me to post more code I'd be happy to do so, but I believe the problem lies in the above code.
Thanks for your time.
When you say:
int writeInt = write(recvInt, buffer, strlen(buffer));
the first parameter of write() should be the socket to write to - you are passing it the count of characters returned by recv()
In addition to Neil's correct answer, you may also want to consider using sendfile()
instead of write()
if it's available on your platform since it provide's a much more efficient means of sending a file than the approach you're taking.
If sendfile()
isn't available you could alternatively use mmap()
to map the file into your process address space and pass the resulting buffer to write()
. This saves you copying the file into a local buffer. However, mmap()
may only provide a gain in the case of large files. As always benchmark your code to see if it's worth using it.
精彩评论