R
Rowena Bellamy
Hi,
I am trying to use the select() statement to check a socket for data
to read. Because I do not know when data will be received I cannot
allow the socket to block.
However, even when the client receives data on the socket, the select
never returns anything but 0 (unless the receive occurs during the
timeout period - if set).
Being a bit of a networking newbie there is probably a much better way
of doing this, any input would be greatly appreciated!
My code is as follows;
if (buf==NULL) {return NULL;}
FD_ZERO(&rread); // clear the fd_set
FD_SET(x->clientsocket,&rread); // indicate that we want to check the
socket only
memset((char *)&to,0,sizeof(to)); // clear the timeval struct
to.tv_sec=0; // timeout select after 0 secs
sr=select(x->clientsocket+1, &rread, NULL, NULL, &to);
if (sr > 0)
{
if (FD_ISSET(x->clientsocket,&rread)) // data is waiting to be read
{
post("data is being read");
while (true)
{
bytesReceived = recv(x->clientsocket, &recvbuffer, 1, 0);
if (recvbuffer == '\n')
{
disconnectsocket(x);
return(pChar);
}
else
{
pChar.push_back(recvbuffer);
}
}
}
else
{
...
}
}
else
{
...
}
Many thanks,
Ro
I am trying to use the select() statement to check a socket for data
to read. Because I do not know when data will be received I cannot
allow the socket to block.
However, even when the client receives data on the socket, the select
never returns anything but 0 (unless the receive occurs during the
timeout period - if set).
Being a bit of a networking newbie there is probably a much better way
of doing this, any input would be greatly appreciated!
My code is as follows;
if (buf==NULL) {return NULL;}
FD_ZERO(&rread); // clear the fd_set
FD_SET(x->clientsocket,&rread); // indicate that we want to check the
socket only
memset((char *)&to,0,sizeof(to)); // clear the timeval struct
to.tv_sec=0; // timeout select after 0 secs
sr=select(x->clientsocket+1, &rread, NULL, NULL, &to);
if (sr > 0)
{
if (FD_ISSET(x->clientsocket,&rread)) // data is waiting to be read
{
post("data is being read");
while (true)
{
bytesReceived = recv(x->clientsocket, &recvbuffer, 1, 0);
if (recvbuffer == '\n')
{
disconnectsocket(x);
return(pChar);
}
else
{
pChar.push_back(recvbuffer);
}
}
}
else
{
...
}
}
else
{
...
}
Many thanks,
Ro