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.sub try_read(socket) buf = "" loop if (IO::Select->can_read socket) buf .= sysread(socket, 1); else exit loop return buf
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Non-blocking socket read on Windows
by salva (Canon) on Feb 13, 2006 at 14:41 UTC | |
by spurperl (Priest) on Feb 13, 2006 at 17:53 UTC | |
Re: Non-blocking socket read on Windows
by BrowserUk (Patriarch) on Feb 13, 2006 at 13:36 UTC | |
Re: Non-blocking socket read on Windows
by zentara (Archbishop) on Feb 13, 2006 at 13:33 UTC | |
Re: Non-blocking socket read on Windows
by eyepopslikeamosquito (Archbishop) on Feb 13, 2006 at 18:26 UTC | |
Non-blocking socket read on Windows (WORKS!)
by wilsond (Scribe) on Jan 12, 2009 at 13:10 UTC | |
by sdetweil (Sexton) on Aug 04, 2009 at 15:00 UTC |