I very appreciate you if you help me.
#! perl5.8.0 -w
use POSIX;
use IO::Socket;
use IO::Select;
use Socket;
use Tie::RefHash;
use strict;
my $port=2345;
my $Type=SOCK_STREAM;
my $server = IO::Socket::INET ->new(
# Listen=>10,
Type=>$Type,
ReuseAddr=>1,
LocalPort=>$port,
Proto=>'tcp'
,Blocking=>0
)
or die "Couldn't be a tcp server on port server_port:$port\n";
my %inbuffer=();
my %outbuffer=();
my %ready =();
tie %ready,'Tie::RefHash';
my $select=IO::Select->new($server);
while (1){
my $client;
my $rv;
my $data="";
foreach $client ($select->can_read(1)) {
if ( $client==$server){
#accept new connection
$client=$server->accept();
my $h=$select->handles();
$select->add($client);
$h=$select->handles();
}else{
#read data
$data='';
$rv =$client->recv($data,POSIX::BUFSIZ,50);
unless (defined($rv) && length $data) {
#this would be the end of
file , so slose the client
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
$select->remove($client);
close $client;
next;
}
$inbuffer{$client}.=$data;
#test whether the data in the buffer or the data we just read
#means there is a complete request waiting to be fulilled.
#If there is , set $ready{$client} to the requests waiting to be fulfilled
while ($inbuffer{$client}=~s/(.*\n)//) {
push (@{$ready{$client}},$1);
}
# push (@{$ready{$client}},$1);
}
}
#Any complete requests to process?
foreach $client (keys %ready){
handle($client);
}
#Buffers to flush?
foreach $client ($select->can_write(50)) {
#Skip this client if we have nothing to say
next unless exists $outbuffer{$client};
$outbuffer{$client}="hello";
$rv=$client->send($outbuffer{$client},0);
unless (defined $rv){
#Whine, but move on
warn "I was told I could write, but I
can't.\n";
next;
}
if ($rv == length $outbuffer{$client}){
#||{! ==POSIX::EWOULDBLOCK) {
substr($outbuffer{$client},0,$rv)='';
delete $outbuffer{$client} unless length $outbuffer{$client};
}else {
#Couldn't write all the data , and it wasn't because it would have blocked.
print "Shut down and move on\n";
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
select->remove($client);
next;
}
}
#Out of band data?
#foreach $client ($select->has_exception(0)) {#arg is timeout #Deal with out-of-band here, if you want to }
#print "end of step in while\n";
}
##handle($socket) deals with all pending requests for $client
sub handle{
#requests are in $ready{$client}
#send output to $outbuffer{$client}
my $client=shift;
my $request;
foreach $request (@{$ready{$client}}) {
print "Inside handle\n";
#$request is the text of the request
# put text of reply into
$outbuffer{$client}
#my $side="South";
#my $hand = send_hand($side);
$outbuffer{$client}=$line;
}
delete $ready{$client};
}
INET.pm
Description: INET.pm
