Using Select()

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
 
J

Jens.Toerring

Rowena Bellamy said:
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.

Since select() (and all the network.related functions you're using)
aren't part of standard C (which is the topic of this group) your
question unfortunately isn't on-topic here. Please send these kinds
of questions to e.g.

comp.unix.programmer

where they are perfectly fine. (BTW, some of the constructs you're
using look more like this is C++, making it even more off-topic for
comp.lang.c.)
Regards, Jens
 
K

Kelsey Bjarnason

[snips]

Hi,

I am trying to use the select() statement to check a socket for data
to read.

That's all well and good, but there is no select() statement in C; it's an
extension added by your implementation's library. As such, it's off-topic
here.

Perhaps comp.unix.programming or comp.windows.programming, etc, would be a
better choice, depending on your implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top