digitalXmage
digitalXmage

Reputation: 147

Why does my Perl script hang when trying to talk to server?

Introduction

I'm following the book "Network programming with Perl" by Stein. This is the 2nd Perl script in the book, where the script attempts to communicate using the correct protocols with a daytime server to get the date and time from the server as a response.

read first-line from remote server

#!/usr/bin/perl 
# file: lgetr.pl 

use IO::Socket; 


my $server = shift; 
my $fh = IO::Socket::INET->new($server); 
my $line = <$fh>; 
printf $line;

Running Script (already made it executable with chmod)

The book then uses this server as an argument to connect to.

wuarchive.wust1.edu:daytime

./lgetr.pl wuarchive.wust1.edu:daytime

The problem

When executed, the program just hangs. I'm guessing the program is waiting for a response from the server. Does this mean that the server doesn't exist any-more? or have typed it incorrectly? How would I get this to work, or would my best bet be creating my own daytime server? (i have a bit of experience of this in C)

Upvotes: 2

Views: 156

Answers (1)

choroba
choroba

Reputation: 241888

Try some of the servers mentioned here: http://tf.nist.gov/tf-cgi/servers.cgi (the first hit on Google for "daytime").

Also, don't use printf where print is enough, especially where there's no format strings.

Moreover, it seems you need the second line, not the first one.

You can use a loop to print all the lines instead:

print while <$fh>;

Works for me with e.g. time.maconbibb.us:daytime.

Upvotes: 5

Related Questions