Perl,这个tcp套接字是服务器还是客户端套接字?

时间:2014-02-02 22:39:14

标签: perl sockets tcp

我想在Android应用程序中写这个,我需要知道我应该使用哪种Java套接字“Client”或Server“。 如果有人可以更多地谈论这段代码,那对我来说会非常有帮助。 据我所理解。该代码创建一个客户端套接字到IP“$ cleintIP”并等待消息。

if (&  eq "OK") {
  print "\nEnabling Shoot&View Mode\n";
  my $ShootAndView = IO::Socket::INET->new(Proto=>"tcp",PeerPort=>5566,PeerAddr=>$CardIP) 
    or die "Can't make TCP socket: $@";

  my ($datagram1,$flags1);

  until(1!=1) {  
    eval {
      local $SIG{ALRM} = sub { die "alarm\n" };
      alarm 5; # Set Timer to check if card is still alive every XX seconds by TCP ECHO (not ICMP!)

      $ShootAndView->recv($datagram1,250,$flags1);
      alarm 0; # if we get a info about new files we disable the alarm here
    };

    if ($@) {
      die unless $@ eq "alarm\n"; #if we got the alarmtime we have to check if card is alive

      #if card is unreachable we have to find it again!
      if (&KeepCardAlive eq "NOK") { 
        print "\nClosing Shoot And View...\n";
        close($ShootAndView);
        print "Back to start...\n";
        goto START;
        };
      }
    else {
      print "\nThere's something new on ", $ShootAndView->peerhost,": $datagram1\n"; 
      #And now for some regex! --> Remove >/mnt from >/mnt/sd/DCIM/200MSDCF/_DSC3233.JPG
      $datagram1 =~ s/^>\/mnt//;

      #system('wget http://admin:admin@192.168.11.254/sd/DCIM/200MSDCF/_DSC3233.JPG');     
      my $wget = join("","wget -bqc http://",$User,":",$Password,"@",$CardIP,$datagram1,);
      system($wget); 
    };
  };
}
else {
  print "\nNothing found. Waiting 10 Seconds...\n";
  sleep(10);
  goto START;
};

2 个答案:

答案 0 :(得分:1)

此代码连接到IP地址,而不是被动地等待连接。这意味着它使用“客户端套接字”。

答案 1 :(得分:1)

服务器套接字是传递给accept而不是connect的套接字。服务器套接字上既不发送也不接收数据;它仅用于监听连接。

当有connectPeerAddr参数时,

IO :: Socket :: INET调用PeerHost。有一个PeerAddr参数,所以它是一个客户端套接字。