ioctlsocket reporting 0 bytes available to read

I'm trying to track down an issue I have with windows sockets.

I have an client and server which I use to send simple text commands data back and forth.

I have a non-blocking socket which I use to send data from my client to the server

int err = send( socket, stringToSend.c_str(), stringToSend.size(), 0 );

I then use the following to check if there is any data on the socket to read:


1
2
3
4
5
6
7
8
9
10
11
if (SOCKET_ERROR != ioctlsocket( socket, FIONREAD, (DWORD*)&nLength ))
{
    if (0 < nLength)
    {
         err = recv( socket, buf, nLength, 0 );

         //blah blah
    }

        //blah blah
}


Now I have a strange problem.
1. When I send the first piece of data from client to server I am able to receive a response from the server.
2. Then I try to send the smae piece of data again, but this time ioctlsocket always reports that 0 bytes is available to read from the socket (nLength is always 0).

The strange thing is that if I use wireshark I can see that the server has actually sent data to the client.

What would cause ioctlsocket to report that nLength = 0 whenever I can see data sent via wireshark???

Thanks
Last edited on
When using sockets you are never guarenteed that the number of bytes you send or receive will be sent all at once. You must put your sends and receives in a loop! This is because even though the IP layer tries to negotiate to use the same number of bytes on each side, they aren't always able to do so.

So, I would change the name of your variable from "err" to "bytesReceived" or something like that.
Topic archived. No new replies allowed.