Прошу прощения, что вклиниваюсь...
Я на перле почти не програмлю, так что сорри если что-то невпопад:
сервер srv.pl:
#!/usr/bin/perl
use IO::Socket;
$srv_sock = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => '4000',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $srv_sock;
$clnt_sock=$srv_sock->accept();
print $clnt_sock "I am here\n";
while (<$clnt_sock>) {
print "Got: ",$_,"\n";
}
close($srv_sock);
#== cut ======================
Клиент cln.pl:
#!/usr/bin/perl
use IO::Socket;
use IO::Select;
use Fcntl qw(:mode);
$sock = new IO::Socket::INET (
PeerAddr => 'localhost',
PeerPort => '4000',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
@a = $sock->stat;
print join(':',@a), "\n";
printf "1. Permissions are o\n", S_IMODE($a[2]), "\n";
print $sock "asdfa sdfa sdf asdf adsf ";
sleep 5;
print "Passed sleep\n";
@a = $sock->stat;
print join(':',@a), "\n";
printf "2. Permissions are o\n", S_IMODE($a[2]), "\n";
print $sock "asdfa sdfa sdf asdf adsf ";
$readfs=new IO::Select();
$readfs->add($sock);
while ($res = IO::Select->select($readfs, undef, undef, 0)) {
printf "1. res=%s\n", $res;
foreach $rh (@$readfs) {
if ($rh == $sock) {
$buf = <$rh>;
if (!length($buf)) {
print "I got 0 bytes to read - supposed connection was closed\n";
exit;
}
printf "Got from server: '%s', %u\n", $buf,length($buf);
}
}
$readfs->remove($rh);
}
print "Prepare to send data\n";
print $sock "Hello, world!\n";
print "Data was sent\n";
close($sock);
#==== cut ====================
1.
Запускаю srv.pl
Запускаю cln.pl
Получаю на клиенте
4:4776:49663:1:1000:1000:0:0:0:0:0:1024:0
1. Permissions are 0777
И спустя пять секунд:
Passed sleep
4:4894:49663:1:1000:1000:0:0:0:0:0:1024:0
2. Permissions are 0777
1. res=3
Got from server: 'I am here
', 10
Prepare to send data
Data was sent
Получаю на сервере:
Got: asdfa sdfa sdf asdf adsf asdfa sdfa sdf asdf adsf Hello, world!
2. Запускаю srv.pl
запускаю cln.pl
Получаю:
4:4946:49663:1:1000:1000:0:0:0:0:0:1024:0
1. Permissions are 0777
Примерно через секунду убиваю сервер.
Еще через четыре секунды получаю:
Passed sleep
4:4946:49663:1:1000:1000:0:0:0:0:0:1024:0
2. Permissions are 0777
1. res=3
Got from server: 'I am here
', 10
1. res=3
I got 0 bytes to read - supposed connection was closed
Т.е. трюк со stat почему-то не сработал, возможно я сделал, что-то не так.
Но select отработал как надо.