From: Mathias B. <mathias@Stacken.kth.se> - 2008-03-25 11:42:16
|
On Tue, 25 Mar 2008, Anil Dhyani wrote: > Hi Michael, > > Attached is the program for your reference i used select statement as per > your guidelines but could not achieve the respective target. I think there > is something i am missing . Please comment. > First, select() will return almost immediately, since you set a select() timeout of 0 by assigning the floating point number 0.5 to tv.tv_sec, which is a long int (casting 0.5 to an int will yield 0). For a 0.5s timeout, try: tv.tv_sec= 0; tv.tv_usec= 500000; /* 5000000µs = 0.5s */ Second, you must test the return value from select() AND the rfds bit set modified by select()! Try something like: fd_set rset, xset; int n; tv.tv_sec= 0; tv.tv_usec= 500000; /* 5000000µs = 0.5s */ FD_ZERO(&rset); FD_ZERO(&xset) /* to handle exceptions on the fd */ FD_SET(fd, &rset); /* for incoming data on fd */ FD_SET(fd, &xset); /* for exceptions on fd */ n= select(fd + 1 , &rset , NULL , &xset , &tv); if (n == 0) { /* timeout after 500000µs */ } else if (n < 0) { /* select() returned an error -- inspect errno */ } if (FD_ISSET(fd, &xset)) { /* exception on fd */ } if (FD_ISSET(fd, &rset)) { /* data available on fd -- use read() to get it */ } > Thanks in advance > Anil You're welcome. The [snip] Regards, Mathias |