服务器端程序代码
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $server_port = 9999;
my $client;
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: $!\n";
while ( $client = $server->accept()) {
print "$client, it is ok\n";
}
close($server);
客户端程序代码:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $remote_host = '192.168.1.241';
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: $!\n";
print "$socket,\t I'm is client\n";
my $answer = <$socket>;
close($socket);
使用方法: 首先运行服务端,然后运行客户端。
阅读(3342) | 评论(0) | 转发(1) |