Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Multiplexed TCP-Client (from: Networkprogramming with Perl)

by strat (Canon)
on Jul 27, 2002 at 15:07 UTC ( [id://185750]=perlquestion: print w/replies, xml ) Need Help??

strat has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I just tried the Example gap5.pl in "A Multiplexed Client" from "Network Programming with Perl" from "Lincoln D. Stein" under Win2k, but it doesn't work.

Perl: Activestate Built 631
OS: Windows 2000 Workstation IO-Select: $VERSION = "1.14";

#! perl use strict; use warnings; use IO::Socket; use IO::Select; use constant BUFSIZE => 1024; my $host = "localhost"; my $port = 80; my $socket = IO::Socket::INET->new("$host:$port") or die $@; my $readers = IO::Select->new() or die "Error: can't create IO::Select read object\n"; $readers->add(\*STDIN); $readers->add($socket); my $buffer = ""; print "starting loop\n"; while(1){ my @ready = $readers->can_read; for my $handle (@ready) { print STDERR "STDIN ready\n"; if ($handle eq \*STDIN) { print "STDIN ready\n"; if (sysread(STDIN, $buffer, BUFSIZE) > 0) { syswrite($socket, $buffer); } else { $socket->shutdown(1); } } if ($handle eq $socket) { print STDERR "SOCKET ready\n"; if (sysread($socket, $buffer, BUFSIZE) > 0) { syswrite (STDOUT, $buffer); } else { warn "Connection closed by foreign host\n"; exit 0; } } # if } # for @ready } # forever
The while-loop is executed, but $readers->can_read never returns anything, so the for my $handle - loop is never started.

Lincoln Stein wrote in the book that ... Because this script doesn't rely on either forking or threading, it runs on practically all operation systems where Perl is available, including the Macintosh.

Does this code really not work with Win2k and AS 631, or do I miss something?

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Replies are listed 'Best First'.
Re: Multiplexed TCP-Client (from: Networkprogramming with Perl)
by kvale (Monsignor) on Jul 27, 2002 at 15:22 UTC
    Your code works fine for me Linux 2.4, perl 5.6.1. Here is how it goes:
    1009% perl index.pl starting loop 45 # <- I typed a 45, then enter STDIN ready STDIN ready STDIN ready SOCKET ready <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>403 Forbidden</TITLE> </HEAD><BODY> <H1>Forbidden</H1> You don't have permission to access / on this server.<P> <HR> <ADDRESS>Apache-AdvancedExtranetServer/1.3.23 Server at xxx.xxx.xxx Po +rt 80</ADDRESS> </BODY></HTML> STDIN ready SOCKET ready Connection closed by foreign host
    Note here that the web server gives 'Permission denied', but it does receive the message.

    -Mark

Re: Multiplexed TCP-Client (from: Networkprogramming with Perl)
by PodMaster (Abbot) on Jul 28, 2002 at 07:32 UTC
    What is it supposed to do?

    From what I saw, works fine for me (without anything running on localhost:80, it dies with IO::Socket::INET: Timeout)

    I don't own lincoln's book.

    update: Anyway, check this out

    # from perldoc IO::Select, w/sprinkles by me use strict; use warnings; use IO::Select; use IO::Socket; my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080); my $sel = new IO::Select( $lsn ); my $new; warn "about to enter while"; while(my @ready = $sel->can_read) { warn "in while"; foreach my $fh (@ready) { warn "in foreach"; if($fh == $lsn) { warn "creating a new socket"; # Create a new socket $new = $lsn->accept; $sel->add($new); warn "added new"; } else { warn "processing socket"; warn "read 10 characters and echo them"; read($fh,$_,10); warn "here they are '$_'"; # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; warn "done with it, neext"; } } } __END__ This is what i got after i ran this script, and then did a\ telnet localhost 8080 hello ther about to enter while at far line 9. in while at far line 11. in foreach at far line 13. creating a new socket at far line 15. added new at far line 19. in while at far line 11. in foreach at far line 13. processing socket at far line 22. read 10 characters and echo them at far line 23. here they are 'hello ther' at far line 25. done with it, neext at far line 30.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

•Re: Multiplexed TCP-Client (from: Networkprogramming with Perl)
by merlyn (Sage) on Jul 27, 2002 at 15:12 UTC

      Ahh! the dangers of being a New Age Open Source Duct Tape Folk Hero

      ' So, I rummaged around for the right event loop or RPC package. I figured this was already a frequently-solved problem, so there was no point in reinventing the wheel. I investigated the Event package, POE, most of the stuff in the RPC:: and IPC:: sections of the CPAN, and was astounded to find that nobody had seemed to tackle this particular problem in a way that I wanted it done. Or at least, in a way that I could easily find it.'

      -- Randal L. Schwartz from 'Getting your kids to do the work' Linux Magazine Aug 2000

      and in next months follow up article

      'OK, by the time I did all this, I could have simply used POE or Event, both found in the CPAN. But going through all the reasons that those features are there was a good exercise for me. Sometimes leverage is the right thing, and in hindsight, I might have appreciated it here.'

      -- Randal L. Schwartz from 'A forking parallel link checker' Linux Magazine Sep 2000

      mitd-Made in the Dark -- President merlyn Fan Club Clearview Twp. Chapter
      'Interactive! Paper tape is interactive!
      If you don't believe me I can show you my paper cut scars!'

        Thanks for pointing those out. And especially prophetic, since my upcoming Oct 2002 LM article is about using POE to do that exact problem again, in a much simpler way.

        -- Randal L. Schwartz, Perl hacker

      ...or he could try to learn something. One does not pick up a book that proposes to teach something without intending to learn it. To that end, this example is comletely justified. "You can't know where you're going if you don't know where you've been" applies here as well. Now, if he was trying to use this as actual code, I'd say use the pre-rolled solution. That's not the impression that I got here.

      thor

        Hello,

        thank you for pointing me to POE, but I'm afraid this is not solving the problem.

        I am new to network programming under Win32, but have got a little bit more experience under Linux or Solaris, esp. with preforking servers and - more in a cargo-cult-manner than with real understanding - POE.

        Some months ago, I played around a little bit with the Multiplexing Server described at How to Write a Chat Server and it was running fine under windows, too.
        Some months ago, I bought the book "Network Programming with Perl", and as I arrived at multiplexing Clients, I thought about writing a client to it (just for fun), threw in the code under Win2k and wondered why it didn't work. I ran it under Linux - no problems. Then I read some documentation again (IO::Select, IO::Socket), and wondered much more.

        This question is not really important for me, but I am very curious how this problem can be solved.

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://185750]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found