Create your own time daemon?

#!/usr/bin/env perl use IO::Socket::INET; # auto-flush on socket $| = 1; # creating a listening socket my $socket = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => '5101', Proto => 'tcp', Listen => 10, Reuse => 1 ); die "cannot create socket $!\n" unless $socket; print "server waiting for client connection on port 5101\n"; while(1) { # waiting for a new client connection my $client_socket = $socket->accept(); # get information about a newly connected client my $client_address = $client_socket->peerhost(); my $client_port = $client_socket->peerport(); print "connection from $client_address:$client_port\n"; # read up to 16 characters from the connected client my $theirtime = 0; $client_socket->recv($theirtime, 16); print "received data: $theirtime\n"; $theirtime =~s/[^\d]//g; # untaint # write response data to the connected client my $their_drift = time - $theirtime ; $client_socket->send($their_drift); # notify client that response has been sent shutdown($client_socket, 1); } $socket->close(); __END__ Problem Your program has forked and you want to tell the other end that you're + done sending data. You've tried close on the socket, but the remote +end never gets an EOF or SIGPIPE. Solution Use shutdown: shutdown(SOCKET, 0); # I/we have stopped reading data shutdown(SOCKET, 1); # I/we have stopped writing data shutdown(SOCKET, 2); # I/we have stopped using this soc +ket

can be tested with:

(perl -e 'print time';sleep 2) | telnet 127.0.0.1 5101 2>/dev/null|tai +l -1

try time+3 to simulate time drifting...

Addendum: a small perl client:

#!/usr/bin/env perl use IO::Socket::INET; # auto-flush on socket $| = 1; # create a connecting socket my $socket = new IO::Socket::INET ( PeerHost => 'localhost', PeerPort => '5101', Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; print "connected to the server\n"; # data to send to a server my $mytime = time; my $size = $socket->send($mytime); print "sent data of length $size\n"; # notify server that request has been sent shutdown($socket, 1); # receive a response of up to 256 characters from server my $response = ""; $socket->recv($response, 256); print "received response: $response\n"; $socket->close(); my $threshold=5; # seconds if(abs($response)>$threshold){ exit 1; }else{ exit 0; }

edit: forgot g in the untaint...


In reply to Re: Time check on group of servers by FreeBeerReekingMonk
in thread Time check on group of servers by shroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.