Solved

ioctlsocket reporting 0 bytes available to read

Posted on 2011-09-15
2
855 Views
Last Modified: 2012-06-21
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 );

Open in new window


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


if (SOCKET_ERROR != ioctlsocket( socket, FIONREAD, (DWORD*)&nLength ))
{
    if (0 < nLength)
    {
         err = recv( socket, buf, nLength, 0 );

         //blah blah
    }

        //blah blah
}

Open in new window



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 no 0 bytes are 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
0
Comment
Question by:Wanting2LearnMan
2 Comments
 
LVL 8

Accepted Solution

by:
ssnkumar earned 250 total points
ID: 36547309
The prototype of ioctlsocket() is like this:
int ioctlsocket(__in     SOCKET s, __in     long cmd, __inout  u_long *argp);

The 3rd argument *argp is "A pointer to a parameter for cmd".
Set *argp to a nonzero value if the nonblocking mode should be enabled
OR
set it to 0 if the nonblocking mode should be disabled.

In your case, I think you are not setting any value to it - you are actually waiting for it to return some value.
So, what might be happening is:
Firsttime when client sends data to server and receives back the response, the value stored in nLength was 0.
And it returned a non-zero positive value back.
Now, when you are calling the ioctlsocket() 2nd time, it is using the non-zero positive value that was set in the previous call.
Because of that, it has become a non-Blocking call and returning immediately.
So, try to explicitly set it to 0 before the 2nd call also and see what happens.

I am assuming that you are checking the value of err returned from ioctlsocket().
It should be 0 to indicate success.
If it is non-zero, then use WSAGetLastError to find the error code - that might throw some clue.

So, the first thing that you should try is to, set nLength to 0 before each call to ioctlsocket, like this:

 
nLength = 0;
if (SOCKET_ERROR != ioctlsocket( socket, FIONREAD, (DWORD*)&nLength ))
{
    if (0 < nLength)
    {
         err = recv( socket, buf, nLength, 0 );

         //blah blah
    }

        //blah blah
}

Open in new window

0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 36548037
the blocking enabling and disabling was done with option FIONBIO and not FIONREAD.

i assume FIONREAD doesn't work properly with non-blocking sockets. you simply should do recv whcih would not block and mostly would return -1 with WSAGetLastError() is WSAEWOULDBLCK.

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now