服务端:
#!/usr/bin/perl -w
use IO::Socket;
my $server_port = 9999;
my $server = IO::Socket::INET->new(LocalPort => $server_port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10) or die "Couldn't be a tcp server on port $server_port: $!
";
#while ( $client = $server->accept())
my $newSocket = $server->accept();
while (<$newSocket>)
{
print $_;
}
close($server);
客户端:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $remote_host = '10.10.17.3';
my $remote_port = 9999;
my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => "tcp",
Type => SOCK_STREAM ) or die "Couldn't connect to $remote_host, $remote_port: $!
";
while (1){
print "$socket, I'm is client
";
print $socket "sucess
";
print $socket "报文已接收
";
sleep(5)
};
my $answer = <$socket>;
#my $newSocket = $socket->accept();
close($socket);