一段IO::Socket的程序,大家给看看!

一段IO::Socket的程序,大家给看看!

大家看看运行是否有问题!


#server端#

#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $port='3000';
my $sock=IO::Socket::INET->new(
   Listen=>SOMACONN,
   LocalPort=>$port,
   Timeout=>60,
   Reuse=>1,
   Proto=>'tcp')
   or die "Create Server ERROR!\n";

warn"Starting server on port $port...\n";

while(1){
     next unless my $session=$sock->accept;
     defined (my $pid=fork()) or die "can't fork\n";

     if($pid==0){
         $sock->close;
          $session->autoflush(1);

          my $line=<$session>;
          print"$line\n";

          print $session "Nice to meet you ,too!\n";
          close $session;
          exit 0;
}else{
      print "Forking child $pid\n";
  }
}
close $sock;

# client端:#

#!/usr/bin/perl

use strict;
use IO::Socket;

my $addr='127.0.0.1';
my $port='3000';
my $buf;

my $sock=IO::Socket::INET->new(
   peerAddr=>$addr,
   peerPort=>$port,
   Proto=>'tcp') or die "Can't connect!\n";

print "$sock Nice to meet you!\n";
$buf=<$sock>;
print"$buf\n";
close $sock;
SOMACONN是啥