// JP opened flex table

Click to See Complete Forum and Search --> : select() function return zero


neik.infology
December 13th, 2007, 09:34 PM
Dear friends,
I am now working on socket programming by C++ in Linux. I have problem when using the select() function.
Here are some line of my codes:

fd = socket(AF_INET, SOCK_STREAM, 0); // create a file descriptor to connect
::connect(fd, (struct sockaddr *) &aServer, sizeof aServer) < 0): connect to the server

//The iterative function to read data from the server
int readData(char* theData, int theLen, int theTimeout)
{
int ret;
fd_set fds;
struct timeval tv;

tv.tv_sec = theTimeout;
tv.tv_usec = 0;

FD_ZERO(&fds);
FD_SET(fd, &fds);

//This select function return 1 for the first time, but return 0 for the second time I call this readData function
ret = select(fd + 1, &fds, NULL, NULL, &tv);
if (ret > 0)
{
ret = ::read(fd, theData, theLen);
.......
....
}

One more important thing that may helpful: the server that I connect to is a network camera, it returns Mpeg video stream by rtsp.

Do you have any idea about the select function above ? I don't know why it returns zero accidentally when the connection is still alive and the whole process is still running properly.

Thank you very much for any reply.

henky@nok.co.id
December 14th, 2007, 09:11 PM
1. Use [CODE] tag for your code, so we can read it easily.

2. Whatever value which select() returns except -1, you need to check
whether select() returns caused by timeout or there is incoming/outgoing
data in file descriptor. Use FD_ISSET() to check whether your socket has
incoming/outgoing data.

//JP added flex table