question about a simple incorrect gab client

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Wu
    New Member
    • Jul 2010
    • 3

    #1

    question about a simple incorrect gab client

    i'm reading Network Programming with Perl
    there is an example in chapter5:concur rent clients.

    Code:
    #!/usr/bin/perl
    use strict;
    use IO::Socket qw(:DEFAULT :crlf);
    
    my $host=shift or die"Usage:gabl.pl host [port]\n";
    my $port=shift || 'tcp';
    
    my $socket=IO::Socket::INET->new(PeerAddr=>$host,PeerPort=>$port) or die "can't connect:$!";
    
    my($from_server,$from_user);
    
    LOOP:
    while(1){
            {
            local $/=CRLF;
            last LOOP unless $from_server=<$socket>;
            chomp $from_server;
            }
            print $from_server."\n";
    
            last unless $from_server=<>;
            chomp($from_user);
            print $socket $from_user,CRLF;
    }
    the author said if we change "last LOOP unless $from_server=<$ socket>" into a loop like
    Code:
    while($from_server=<$socket>){
       chomp $from_server;
       print $from_server,"\n";}
    this will cause a deadlock,this is what the author say
    " Now the script hangs after it reads the first line from the server. The FTP server is waiting for us to send it a command, but the script is waiting for another line from the server and hasn't even yet asked us for input, a situation known as deadlock. "
    I just don't understand why the script hangs after it reads the first line from the socket if the socket contains more than one line.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Well, as the author has stated, if the FTP server is waiting for a command to be issued and at the same time the server has noticed that it hasn't actually been asked for input, its a deadlock.

    Comment

    Working...