I'm having a bit of trouble figuring out how to detect if a socket is closed or not. Outside of using read()/write(). I also don't want to set the socket to non-blocking if at all possible.
I wrote a function that reads a single char(or byte, whatever) and wanted to use it in a simulated blocking and nonblocking way.
It works fine for waiting for the correct timeouts, but since I use ioctl() to detect if there is data to be read, I won't actually call the read() method that would show me the socket is closed.
I was thinking there was some way to detect "can write" or "can read" on a file descriptor without going into a blocking state, but I can't find it if there is one.
[code=c]
read_ret_t ReadChar(int sock, int milisecondTimeo ut, char &refchar)
{//-1=errors, 0=closed, 1=timeout, ?=ok?
enum read_ret_t retval=ERRORED ;
char buf[10]={0x00};
char nbyte[10]={0x00};
int numofwaits=0;
if(milisecondTi meout!=0)
{// "non-blocking"
refchar=0x00;
while(numofwait s<milisecondTim eout)
{
int bytestoread=0;
int d=ioctl(sock, FIONREAD, &bytestoread );//should do error checking on return value of this
if(d!=0)printf( "ioctl()=%d\r\n ",d);
//int fd=write(sock,n byte,1);//writing a single 0x00 byte can trigger fd=-1 when closed
if(bytestoread> 0)
{
int res=read(sock,b uf,1);
if (res<0)
{retval=ERRORED ;}
else if(res==0)
{retval=CLOSED; }
else
{retval=OK;}
refchar=buf[0];
break;
}
else
{
usleep(1000);//1000uSecs = one milisec
numofwaits=numo fwaits+1;
}
}
if (numofwaits>=mi lisecondTimeout )
{
retval=TIMEDOUT ;
}
}
else
{//blocking
int res=read(sock,b uf,1);
if (res<0)
{retval=ERRORED ;}
else if(res==0)
{retval=CLOSED; }
else
{retval=OK;}
refchar=buf[0];
}
return retval;
}
[/code]
Any thoughts on a non-blocking way to detect if a blocking call has timed out?
I toyed with changing the socket option of receive timeouts to a very small time and using like recv with the PEEK option, but that seemed like the wrong way to go.
I wrote a function that reads a single char(or byte, whatever) and wanted to use it in a simulated blocking and nonblocking way.
It works fine for waiting for the correct timeouts, but since I use ioctl() to detect if there is data to be read, I won't actually call the read() method that would show me the socket is closed.
I was thinking there was some way to detect "can write" or "can read" on a file descriptor without going into a blocking state, but I can't find it if there is one.
[code=c]
read_ret_t ReadChar(int sock, int milisecondTimeo ut, char &refchar)
{//-1=errors, 0=closed, 1=timeout, ?=ok?
enum read_ret_t retval=ERRORED ;
char buf[10]={0x00};
char nbyte[10]={0x00};
int numofwaits=0;
if(milisecondTi meout!=0)
{// "non-blocking"
refchar=0x00;
while(numofwait s<milisecondTim eout)
{
int bytestoread=0;
int d=ioctl(sock, FIONREAD, &bytestoread );//should do error checking on return value of this
if(d!=0)printf( "ioctl()=%d\r\n ",d);
//int fd=write(sock,n byte,1);//writing a single 0x00 byte can trigger fd=-1 when closed
if(bytestoread> 0)
{
int res=read(sock,b uf,1);
if (res<0)
{retval=ERRORED ;}
else if(res==0)
{retval=CLOSED; }
else
{retval=OK;}
refchar=buf[0];
break;
}
else
{
usleep(1000);//1000uSecs = one milisec
numofwaits=numo fwaits+1;
}
}
if (numofwaits>=mi lisecondTimeout )
{
retval=TIMEDOUT ;
}
}
else
{//blocking
int res=read(sock,b uf,1);
if (res<0)
{retval=ERRORED ;}
else if(res==0)
{retval=CLOSED; }
else
{retval=OK;}
refchar=buf[0];
}
return retval;
}
[/code]
Any thoughts on a non-blocking way to detect if a blocking call has timed out?
I toyed with changing the socket option of receive timeouts to a very small time and using like recv with the PEEK option, but that seemed like the wrong way to go.
Comment