- 论坛徽章:
- 0
|
程序能够正常运行但不能够达到预期的目的,即如果两者的最后修改日期不同,就把服务端的文件传送到客户端。
server.pl
- #!/usr/bin/perl -w
- use strict;
- use Socket;
- use constant PORT=>;"7986";
- my $protocol=getprotobyname('tcp');
- $SIG{'INT'}=sub{close SOCK;exit 0;};
- socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "SOCKET fail:$!\n";#建立SOCKET套接字
- setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1) or die "Can't set SO_REUSEADDR:$!\n";
- #设置服务器的IP地址可重复使用#
- my $addr=sockaddr_in(PORT,INADDR_ANY);#生成套接字地址
- bind(SOCK,$addr) or die "bind() fail:$!\n";#绑定地址
- listen(SOCK,SOMAXCONN);#监听端口
- warn "Waitting for incoming connect on port PORT......\n";
- while(1)
- {
- next unless accept(SESSION,SOCK);#如果没有连接,进行下一循环
- my $md_time=<SESSION>;;
- if($md_time=~/time:/)
- {
- print SESSION "sending";
- my $time=(stat("data.txt"))[9];#得到此文件的最后修改日期
- last if($_ eq $time);#如果两者最后修改时间相同则退出
- open(FILE,"data.txt") or die "open file error:$!\n";
- foreach my $line (<FILE>;)
- {
- chomp $line;
- print SESSION $line;
- }
- close FILE;
- }
- }
- close SOCK;
复制代码
client.pl
- #!/usr/bin/perl -w
- use strict;
- use Socket;
- use IO::Handle;
- my $ip=shift||"127.0.0.1";
- my $protocol=getprotobyname('tcp');
- my $port="7986";
- my $host=inet_aton($ip);
- my $sock_addr=sockaddr_in($port,$host);
- socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "SOCK fail:$!\n";
- connect(SOCK,$sock_addr) or die "connect fail:$!\n";
- #warn "connecting......\n";
- SOCK->;autoflush();
- if(-e "newdata.txt")
- {
- print SOCK "time:"."(stat(\"data.txt\"))[9]";#把此文件的最后修改时间传送给服务端软件
- }
- else{
- print "no file";
- print SOCK "time:";
- }
- my $state=<SOCK>;
- chomp $state;
- if($state!~/sending/){exit 0;}
- warn "Reciving......";
- while(<SOCK>;)
- {
- open(FILE,">;data.txt") or die "open file fail:$!\n";
- chomp $_;
- print FILE "$_"."\n";
- }
- close FILE;
- close SOCK;
复制代码 |
|