Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print contents of file on remote server to current server 1

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
I have two Perl scripts, server.pl and client.pl, that will print output from the client on the server.

However when the end of the file is reached I have to ctrl-C to get the next prompt instead of it closing the file and moving on with the rest of the program.

How can I get it to move on without doing a ctrl-c?

client.pl
Code:
#!/usr/bin/perl

use IO::Socket;
my $sock = new IO::Socket::INET (
                                 PeerAddr => 'localhost',
                                 PeerPort => '7070',
                                 Proto => 'tcp',
                                 );
die "Could not create socket: $!\n" unless $sock;
$data_file = "/tmp/file.dat";
open(DAT, "<$data_file") || die "Could not open file: $!\n";
for (;;) {
        while (<DAT>) {
                my $msg = $_;
                chomp $msg;
                print $sock "$msg\n";
                }
        sleep 1;
        DAT->clearerr();
}
close(DAT);
close($sock);
 
Code:
[red]for (;;) {[/red]
        while (<DAT>) {
                my $msg = $_;
                chomp $msg;
                print $sock "$msg\n";
                }
        sleep 1;
        DAT->clearerr();
}

This is an infinite loop; it will never end (unless you use a break to stop it).

The reason you need to Ctrl-C is because the program is stuck in this infinite loop.

Why is it even necessary?

Code:
        [blue]while (<DAT>) {[/blue]
                my $msg = $_;
                chomp $msg;
                print $sock "$msg\n";
                }

This will keep looping until the end of the file is reached, and THEN the rest of your code past this block will continue.

There is no need for the outer infinite loop.

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top