|Pathologically Eclectic Rubbish Lister|
Non-blocking socket read on Windowsby spurperl (Priest)
|on Feb 13, 2006 at 07:51 UTC||Need Help??|
spurperl has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to make a Perl script talk in a bi-directional manner with a C++ application using a socket. For this, I want to implement non-blocking socket read. Now, there is the nice IO::select which has the can_read() function. However, can_read() doesn't actually return how much I can read, only that I can. So, I've been reading the Cookbook and the Advanced Perl book, and my options are: (1) use multi-threading and (2) using fcntl() to make sysread() on the socket non-blocking. As you probably know, the nonblocking fcntl() (option 2) doesn't work on Windows, which apparently doesn't support this mode of work with files. I know I can go to option (1), but I would prefer not to - because my script is really quite simple. So, I came up with the following approach (pseudocode):
This works, because if can_read says that there's data to read, there's at least one byte there, so I sysread with length 1 and concatenate the byte. When the data ends, the loop ends. Quite, simple.
Problem is, it looks inefficient. If I receive a packet of several KB, this loop will happily run thousands of times, each time calling can_read() and sysread(). However, I don't see any other option for robust non-blocking read on Windows, without using threads.
Any ideas / suggestions / enlightments ?
Thanks in advance