I’m recently learning how to program a basic webserver in c. My server depending on certain inputs will send various lines of text all which end in a blank line, and I need to recieve them from the client side somehow to display one line at a time.
Right now the server does something like
send(socket, sometext, strlen(sometext), 0)
send ...
send(socket, "\n", 2, 0);
....
Client:
Currently what I am doing is using fdopen to wrap the socket file descriptor and read with fgets:
FILE *response = fdopen(someSocket, "r");
while(1){
fgets(input, 500, response);
if(strcmp(input, "\n") != 0){
//do some stuff
}
else {
// break out of the loop
break;
}
}
This all works well except my program need to fulfil one condition, and that is to not break the socket connection ever. Because I opened a file, in order to stop memory leak the next line after is:
fclose(response);
This however is the root of my problems because it not only closes the file but also closes the socket, which I can’t have. Can anyone give me any guidence on simple methods to work around this? I was thinking about maybe using recv() but doesn’t that have to take in the whole message all at once? Is there any way to recreate the behavior of fgets using recv?
I’m really a newbie at all this, thanks for all the help!
Your only real alternative is to write a function that:
1) calls recv() with a fixed buffer and a loop,
2) only returns when you get a complete line (or end-of-connection)
3) saves any “leftover characters for the next read of the next line of text
IIRC, Stevens has such a function:
Book:
http://www.amazon.com/Unix-Network-Programming-Sockets-Networking/dp/0131411551/
Example code:
http://kohala.com/start/unpv12e.html
PS:
Any self-respecting higher-level web programming library will give you a “getNextLine()” function basically for free.