I have a simple client/server model setup with 2 scripts. The client connects to the server, sends some data, and then closes the connection. The server sits and receives the input over the socket. I want to have the server detect when the connection has been closed. I've been trying to use the connected function, but it is not working properly (prob. not using it correctly). I am using ActiveState for Windows.
Here is the code:
server:
use IO::Socket;
use IO::Select;
$read_set = new IO::Select(); # create handle set for reading
$a = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => '80',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
$read_set->add($a);
$quit = 0;
while ($quit == 0) {
foreach $rh ($read_set->can_read(20)) {
print "start\n";
my $new_sock = $rh->accept();
$temp = $new_sock->connected();
$temp2 = $rh->connected();
print "-$temp--$temp2-\n";
if ( not defined($new_sock->connected) ) {
$quit = 1;
last;
}
while(<$new_sock>) {
print $_;
}
if ( not defined($new_sock->connected) ) {
$quit = 1;
print "not defined anymore\n";
}
sleep 3;
print "end\n";
$temp = $new_sock->connected();
$temp2 = $rh->connected();
print "-$temp--$temp2-\n";
}
}
close($sock);
client:
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => 'localhost',
PeerPort => '80',
Proto => 'tcp',
Timeout => 10,
);
die "Could not create socket: $!\n" unless $sock;
while ($i < 5) {
$i++;
print $sock "sending the stuff $i\n";
print "sending the stuff $i\n";
sleep 2;
}
$sock->close();
sleep 20;
Is this even the right way to go about this?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.