加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl 的 Socket 客户端获取服务器时间

发布时间:2020-12-15 21:08:15 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/local/bin/perl -Twrequire 5.6.0;use Socket;use FileHandle;use strict;my($remote,$port,@thataddr,$that,$them,$proto,@now,$hertime);pri

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/local/bin/perl -Tw
require 5.6.0;
use Socket;
use FileHandle;
use strict;
my($remote,$port,@thataddr,$that,$them,$proto,@now,$hertime);

print "clientn";
$remote = shift || 'localhost' ;
$port = 99999 ;  # timeserver is at this port number
@thataddr=gethostbyname($remote);

$that = pack('Sna4x8',AF_INET,$thataddr[4]);

$proto = getprotobyname('tcp');
if ( socket(SOCK,PF_INET,SOCK_STREAM,$proto ) ){
     print "Socket ok.n";
}
else { 
     die $!; 
}
if (connect(SOCK,$that)) {
     print "Connect ok.n";
}
else {
     die $!;
}
SOCK->autoflush;
$hertime = <SOCK>;
close(SOCK);
print "Server machine time is: $hertimen";
@now = localtime($hertime);
print "tTime-$now[2]:$now[1] ","Date-",$now[4]+1,"/$now[3]/$now[5]n";

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读