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.
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.