Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

sockets: problems with daytime client

by 7stud (Deacon)
on Jan 20, 2010 at 09:26 UTC ( [id://818404]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I get a "Connection refused" error from this code:

use strict; use warnings; use 5.010; use IO::Socket; my $host = 'localhost'; my $port = 'daytime(13)'; #Error on this line: my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerHost => $host, PeerPort => $port, ) or die "cannot connect: $!"; while (<$sock>) { print; }

I've found several examples of code on google that use localhost as the host, but it doesn't seem to work for me. I'm using mac osx 10.4.11.

I can't seem to find a public daytime server either.

Replies are listed 'Best First'.
Re: sockets: problems with daytime client
by gmargo (Hermit) on Jan 20, 2010 at 15:53 UTC

    I can't seem to find a public daytime server
    A Google search for public daytime server rapidly finds the National Institute of Standards and Technology, which runs NTP and DAYTIME servers.

    See Set Your Computer Clock Via the Internet and NIST Internet Time Servers.

    Your next challenge is to write a DAYTIME server, and configure launchd on your own system to run it on demand.

    The following modification of your code will parse the NIST standard DAYTIME format.

    use strict; use warnings; #use 5.010; use IO::Socket; #my $host = 'localhost'; #my $host = 'time.nist.gov'; #my $host = 'nist1-sj.ustiming.org'; my $host = 'time-nw.nist.gov'; # pester microsoft my $port = 'daytime(13)'; my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerHost => $host, PeerPort => $port, ) or die "cannot connect: $!"; while (<$sock>) { s/^[[:space:]]+//; s/[[:space:]]+\z//s; next if /^$/; # NIST Daytime Protocol format # http://tf.nist.gov/service/its.htm # JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM if ($_ =~ /^(\d+)\s+(\d{2})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{ +2})\s+(\d{2})\s+(\d)\s+(\d)\s+([0-9\.]+)\s+UTC\(NIST\)\s+(.)/) { my ($mjd, $yr, $mo, $da) = ($1,$2,$3,$4); my ($hour, $min, $sec, $dst, $lsec) = ($5,$6,$7,$8,$9); my ($health, $msADV, $otm) = ($10,$11,$12); print "NIST: $yr-$mo-$da $hour:$min:$sec\n"; } else { print "$_\n"; } }

      Before asking my question here, I visited all the links in your post while looking for public daytime servers, but I couldn't get any host to work. My program just hung. Now things seem to be working.

      All I was trying to do was retrieve a line of text from a remote host to see if I could get a simple networking program to work.

      Thanks.
        mahbe should have tried HTTP instead, your browser conn seems to work :)
Re: sockets: problems with daytime client
by zentara (Archbishop) on Jan 20, 2010 at 12:27 UTC
    Google for htpdate, or check out this, where you can get fairly accurate time from google servers.
    #!/usr/bin/perl use warnings; use strict; use IO::Socket; # su to root first, unless sudo is setup correctly for the commands my $datecommand='date -s'; #'set system time' command my $request ="HEAD / HTTP/1.0\r\nHost: \r\nUser-Agent:zzzdate\r\nPragm +a: no-cache\r\n\r\n"; my $socket = IO::Socket::INET->new ( Proto => 'tcp', PeerAddr => 'www.google.com', PeerPort => 80 ) or die "Can't connect $!\n"; # Send HEAD requests print "$request\n"; print $socket $request; while (defined (my $res = <$socket>) ) { print "$res\n"; if ( $res =~ /Date: / ) { # Like-> Date: Wed, 22 Oct 2008 18:30:46 GMT $res =~ s/Date: //; #print "$res\n"; print "Setting time... ", `$datecommand \"$res\"`; } } close $socket; #zero out adjtime if time keeps getting thrown off unexpectedly #open (TH,"> /etc/adjtime") or die "$!\n" #close TH;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: sockets: problems with daytime client
by Anonymous Monk on Jan 20, 2010 at 09:35 UTC
    Um, "connection refused" is showing that it works, you're simply not welcome.
      It's my computer, and I have full admin privileges, so I would like to welcome my script with open arms.
        So you are connecting to your own machine and asking for information from daytime. In that case, you need to be running a daytime service. 'Connection refused' means any of several things, including that there is nothing listening on the port that you connected to (or, per AM's comment, that you are being blocked or ...). OS X does not provide such a service by default. IOW, you also need to find code for a daytime service (or write your own) that listens on port 13.
        You are at your home, and ask Bob for the time. There is no response, and a light bulb goes off in your head saying "Connection refused". Being at home, you have full admin privileges, but that means nothing to Bob. Is bob even in your house?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (None)
    As of 2024-04-18 23:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found