#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
my $udpa = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerPort => 5000,
);
unless ($udpa) {
die "$!";
}
$udpa->print("aaaaaaaaaaaaaaaaaaa\n");
print $udpa->getline;
$udpa->close;
use IO::Socket;
use IO::Select;
$port = shift || 5000;
$listening_socket = IO::Socket::INET->new(LocalPort => $port,
Listen => SOMAXCONN,
Proto => 'tcp',
Reuse => 1,
);
if ( ! $listening_socket ){
die "listen err$!\n";
}
$selecter = IO::Select->new;
$selecter->add($listening_socket);
while (1){
($active_socks) = IO::Select->select($selecter,undef,undef,undef);
foreach $sock (@{$active_socks}){
if ( $sock == $listening_socket ){
print "NEW CLIENT\n";
$new_sock = $listening_socket->accept;
$selecter->add($new_sock);
} else {
$buf = <$sock>;
if ( $buf ){
chomp $buf;
print "NEW MESSSAGE\n";
print $buf . "\n";
print $sock $buf . "\n";
$sock->flush();
} else {
$selecter->remove($sock);
$sock->close();
}
}
}
}