Never use
gets()
for reading binary data: it will swallow
'\n'
bytes, and you will have trouble interpreting the data if it contains
0
-bytes. Even worse, you are risking a buffer overflow if you can't guarantee your buffer is big enough to hold all data up to and including the next occurence of
'\n'
!
gets()
is meant for getting printable text, not binary data. See
http://www.cplusplus.com/reference/cstdio/gets/[
^]
[edit]
After you posted your code, the question becomes much clearer: You have inserted calls to
gets()
in addition to your original socket reads, not replaced those calls by
gets()
as I thought!
Obviously, calling a second - unrelated - reading function does not serve any real purpose. But since that change appeared to make your program work, maybe the calls to gets() have a hidden side effect, e. g. forcing a synchronization of your socket.
I have no experience with socket programming, but I believe it works different on different systems, so you should mention what OS you're working on!
In any case, you really should check for errors, especially after the call to recv. Check the appropriate documentation for socket programming on your OS.
[/edit]
P.S.:
Have a look at this:
http://stackoverflow.com/questions/6979769/linux-ioctl-with-fionread-always-0[
^] Your problem may be related to what is explained in the second response - that you should use
select()
rather than
ioctl()