Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ioctlsocket(...) don't work properly.

Status
Not open for further replies.

sulacco

Technical User
Nov 8, 2002
74
RU
Does anybody know why ioctlsocket don't work properly.
It must place how much bytes is waiting in socket.
When I place breakpoint to ioctlsocket then this function place how much
bytes is waiting in i_HowMuch. But if I remove breakpoint then in
i_HowMuch just zero. Why that? What's the problem.
I'm using ioctlsocket(...) to know beforehand how much bytes are waiting
me so I can create dynamic buffer.


if(send(ServerSock, Retro, strlen(Retro), 0)==SOCKET_ERROR){

i_ErrorSock=WSAGetLastError();
return false;
}
//memset(sBuf,0,sizeof(sBuf));
if(ioctlsocket(ServerSock,FIONREAD,&i_HowMuch)==SOCKET_ERROR){

i_ErrorSock=WSAGetLastError();
return false;
}
 
> But if I remove breakpoint then in
> i_HowMuch just zero. Why that? What's the problem.
You're not waiting long enough for an answer to come back

> if(send(ServerSock, Retro, strlen(Retro), 0)==SOCKET_ERROR){
Send some data off to some remote location (this will take time - lots of time in comparison to the speed at which code executes at).

You've broken http://java.sun.com/people/jag/Fallacies.html]Rule Number 2[/url]

> if(ioctlsocket(ServerSock,FIONREAD,&i_HowMuch)==SOCKET_ERROR){
Determine how much information has been received so far.
With a breakpoint, you give the remote a nice long time (many seconds) to reply and hence you get the answer you expect. Charging through at light-speed without a breakpoint gives the remote no chance whatsoever to respond.

You need to wait in a loop (or go do other things as well) for the reply to come back. You also need to be prepared for the reply never coming back.

--
 
Thanks. I used Sleep(...) but i've got gut feeling this isn't professional.
 
What about this one:

u_long i_HowMuch=0;

while(!i_HowMuch){
if(ioctlsocket(ServerSock,FIONREAD,&i_HowMuch)==SOCKET_ERROR){
i_ErrorSock=WSAGetLastError();
return false;
}
}
will be it enough? But then again what if the loop will be infinite?
 
Please use TGML [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

Yes, the loop could be infinite
So add a timeout
Code:
u_long i_HowMuch=0;
u_long i_Timeout=0;

while(!i_HowMuch && i_Timeout < 10 ){
    if(ioctlsocket(ServerSock,FIONREAD,&i_HowMuch)==SOCKET_ERROR){
        i_ErrorSock=WSAGetLastError();
        return false;
    }
    Sleep( 1000 );  // sleep for a second
    i_Timeout++;    // bump the count
}
if ( i_Timeout == 10 ) {
    // set some other error condition
    return false;
}

--
 
Thanks, Salem.
checking code tags...
<code>index++;</code>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top