recv() stops when there's exactly 508 bytes left.

Started by
16 comments, last by hplus0603 13 years, 1 month ago
Make sure you implement Erik's logic from post #4 for the send code as well on the server. Specifically, you need to make sure -1 is not returned on the send logic because you might be filling up the client's send buffer too fast on the server.

To help test that as well, you can increase the size of the send/recv buffers on sockets as well as disable nagle's algorithm. Check out the setsockopt function as well as the SO_RCVBUF, SO_SNDBUF, SO_LINGER, and TCP_NODELAY settings. You might need to Google for a few specific examples to read up on which settings might be best to try for your specific application.

Lastly, make sure you don't have any bugs in the actual debugging output statements you are using. I.e., I've done silly things in the past where I outputted a size before I modified it correctly so it always looked like there was data left, but there really wasn't. I'm not sure what your specific code looks like right now after modifications, but the last debug output seemed not to print enough variable information.
Advertisement

As the topic states, my recv() call keeps stopping when there is 508 bytes left to read. I'm trying to send a file, and it's all going good untill we hit the magical 508 bar.



People already talked about the while() loop on the sending side.
How do sender and receiver communicate the file size in the first place? If you print it before the while() loop on each side, do they agree?
enum Bool { True, False, FileNotFound };
The loop already checks for errors, and disconnects. But it never gets there, because recv() seems to wait for data thats not there.

Drew, i'll take a closer look at what you wrote when i get home.

Hplus, the client will send the file size and name as a structred packet. The server picks up on this and expect the next input on the wire to be the actual file. If i'm printing the file size on both server and client, theh are the same.

I should note that smaller files that only takes up a few loops, get through without problem.'i'll post the updated functions when i get home.

Thanks for all the awesome help so far :)
Omg, zombie! Zomgbie.
You should get into the habit of posting your actual code, as the problem might very well be clearly visible there, and not pretend code that might not contain the same error. Include the parts that deal with sending the file size etc. before the file loop also.

You should get into the habit of posting your actual code...


Point taken! I've updated my first post with the two actual functions that takes care of sending/recv.

Edit: The formatting is a bit off, sorry for that.
Omg, zombie! Zomgbie.
We still can't see your socket setup, and I don't see the server's socket being flushed or closed as previously suggested. If you could develop a minimal client (all in one file) that demonstrates the behaviour that would be easiest for us (as we can test it in isolation and see everything).
How does recvCommand(client->clientSocket, NET_FILEINFO, buffer) work?
Make sure you don't read more than the number of bytes sent by the server's sendCommand(NET_FILEINFO, (char *)&info), or you might get part of the file in 'buffer' in that read. If you investigated the file-contents to be correct even when you don't receive the entire file that shouldn't be possible, but it seems the most likely issue from your code, so I would double-check that.

Hplus, the client will send the file size and name as a structred packet. The server picks up on this and expect the next input on the wire to be the actual file. If i'm printing the file size on both server and client, theh are the same.


Are you sure that you're not accidentally reading more than was put in there for the file size packet?
Note that TCP is a stream protocol, not a packet protocol. You have to packetize and re-buffer yourself.
If you check the contents of the file that is received, are you missing the FIRST 508 bytes, or the LAST 508 bytes?
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement