服务器采用单线程处理,通过multiplexed监听网络IO,如果有新的客户连接请求,将其加入到select监听;当客户的请求处理完毕之后,从其从select监听中删除。 use strict; use warnings; use IO::Socket; use IO::Select; my $host = "192.168.1.102"; my $port = 27002; my %SESSION; my $socket = IO::Socket::INET->new(Listen => 20, Reuse => 1, LocalPort => $port, ); die "can not create socket:$!" unless $socket; my $reader = IO::Select->new or die "can not create select object:$!"; $reader->add($socket); warn "waiting for incoming connection..../n"; while(1){ my @ready = $reader->can_read; for my $handle (@ready){ if($handle eq $socket){ my $connection = $socket->accept; $SESSION{$connection} = $connection; $reader->add($connection); } elsif($SESSION{$handle}){ my $user_input; while(my $bytes = sysread($handle,$user_input,1024)){ chomp($user_input); print $user_input . "/n"; } $reader->remove($handle); close($handle); delete $SESSION{$handle}; } } } 使用于Windows和Ubuntu两个操作系统