Howdy. I am in a networking class and we are creating our own "networking API" using the socket functions we have learned in class thus far.
For the assignment the professor provided the already complete chat and server programs and we were to fill in a blank .c file that had an associated header file that described the function calls in our little "networking API" that the chat and server programs used.
I am close to done but have ran into a problem. I was gonna go in and mess with my code, but all the other parts of my "networking API" work so I didn't want to to screw with it when I am so close to done and have a little change cause other parts not to work. Plus I am not 100% sure that I do in fact know what my bug is.
My problem is with my function called recv_line that is supposed to mimic recv().
My function recv_line gets handed the file descriptor from the client for the connection that was setup with the server. I then go ahead and associate a stream with the file descriptor using fdopen. The bit of code looks like:
// Associate a stream with the sock_fd
if (NULL == (fdstream = fdopen(sock_fd, "r"))) {
return -1;
}
Now this is where I believe my bug to be but I am not 100% sure as I said. Firstly, the first time I use my recv_line function it works great. It does in fact recv_line the line as expected. But the problem arises the second time around.
I stepped through my function with GDB and the above bit of code does excute without problems. I then go on to peel off a character from my stream (using fgetc) and check if it is equal to the EOF char. If it is I return, else I will process it. The second time that I call recv_line it returns cuse it read the EOF char every time.
I'm assuming this is happening because the data sent to the client is in the original stream (the one created the first time around) and the second time I call fdopen there isn't any data in the second stream I created? I'm not completely sure how fdopen works when I call it the second time around?
On I side note I my reading of my data this way as we are supposed to continue reading data until we read an EOF or the character sequence \r\n. I use getc and ungetc with the stream to provide a look ahead mechanism that will check for the EOF or the \r\n sequence.
So with all the being said, does anyone know what exactly is going on with my big of code and program? I Googled around for a method to check if a FD has an associated stream to it and wasn't successfully in finding/seeing anything. I did some research to see if there was some fstat time function that would let me see associated steams and came up with nothing. Can I just dup the FD that is passed in to the function recv_line and then fdopen on the dupped FD and avoid all these problems?
Thanks for all the help. I am sorry if I wasn't clear enough. I will do my best to clarify anything if you ask for me to do so. And sorry my question was so lengthy. =*(
Thanks again though. I really appreciate your help.