in reply to Re: Perl socket server problem
in thread Perl socket server problem

Lots of things said in the first reply is actually wrong. select does not have the magic to redirect standard output.
Shocking! pg is usually right, but the first reply is completely accurate and highly educational! Here is a version of the original program with timestamps added:
$queuelength=1; use Socket; socket(HANDLE, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $addr = sockaddr_in(80, INADDR_ANY); bind(HANDLE, $addr); listen(HANDLE,$queuelength); while(true) { print "hallo1\n"; print STDERR "before accept ", scalar(localtime), "\n"; accept(NEW_HANDLE, HANDLE); print STDERR "after accept ", scalar(localtime), "\n"; select(NEW_HANDLE); print STDERR "after select ", scalar(localtime), "\n"; $line=<NEW_HANDLE>; print STDERR "after readline ", scalar(localtime), "\n"; print $line; print STDERR "after print ", scalar(localtime), "\n"; print "hello2\n"; } __END__ Output: before accept Sat Oct 11 16:01:55 2003 after accept Sat Oct 11 16:02:44 2003 after select Sat Oct 11 16:02:44 2003 after readline Sat Oct 11 16:02:44 2003 after print Sat Oct 11 16:02:44 2003 before accept Sat Oct 11 16:02:44 2003 after accept Sat Oct 11 16:03:12 2003 after select Sat Oct 11 16:03:12 2003 after readline Sat Oct 11 16:03:12 2003 after print Sat Oct 11 16:03:12 2003 before accept Sat Oct 11 16:03:12 2003
Note that the only pauses are for the accept() call, not select(). And you can see a screen shot with two browser windows as well as the perl program in a DOS box here. The select() call redirects the standard output to a socket which sends is connect to a browser.

Replies are listed 'Best First'.
Re: Re: Re: Perl socket server problem
by Fletch (Bishop) on Oct 12, 2003 at 01:35 UTC

    <pedantic>
    Technically speaking one arg select() doesn't redirect standard out, it simply changes the default output handle (i.e. which handle print() will write to if you don't give it an explicit one). The standard out file descriptor (as well as the STDOUT filehandle) is unchanged and still points to the same place before and after select(SOMEOTHERHANDLE) has been called. If you fork/exec something else it's stdout will be the same as the parent, not the select'd handle.
    </pedantic>

      <evenmoreso>
      Not so "simply", it also has a return value and a side effect. See Symbol::geniosym for an interesting use of both.
      </evenmoreso>
Re: Re: Re: Perl socket server problem
by pg (Canon) on Oct 11, 2003 at 20:36 UTC

    Thanks for being frank and straight with me.

    My apology and I realized that part of the confusion is that, there are two select functions in Perl, and that might be part of the problem in the original program. I don't want to guess whether he used the right select, or the wrong select.

    Any way, it is worth to mention that: 1) The first type of select takes a file handler as parameter, and does what Thelonius described; 2) A second one is the select system call, which determines whether the socket is ready for certain actions (this is what I meant in my original reply, and this one does not work for windows.)

    Great, we have all straightened something!

    Using select call in this context made me right the way rushing to the interpretation that he was making the system call...;-) well maybe he was.

    Anyway autoflush is a major part of the problem, so 1) do autoflush; or 2) $|++; or 3) \n...