perl socket编程实例之简单客户端代码

发布时间:2019-08-26编辑:脚本学堂
本文介绍一例perl socket编程的实例代码,学习使用socket技术开发一个简单的客户端,有需要的朋友不妨参考下。

perl socket实例之实现简单的客户端。
代码如下:   
 

复制代码 代码示例:

#!/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, $port, $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";