Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket detect client disconnect

Status
Not open for further replies.

weasel669

Programmer
Joined
Apr 2, 2004
Messages
3
Location
US
I'm working on a small test server that displays "hello world" on both the server screen and the client screen. my problem is when i disconnect the client "ctrl-c" the server doesn't know this. how do i detect that the client has disconnected so the server can reset itself and wait for a new connection?

Code:
#!/usr/bin/perl -w
use IO::Socket;

$PORT = 3334;

$server = IO::Socket::INET->new( Proto     => 'tcp',
                                 LocalPort => $PORT,
                                 Listen    => SOMAXCONN,
                                 Reuse     => 1)

	or die "can't setup server" unless $server;
	
print $server->sockport."\n";

while (my $client = $server->accept()) {
	$client->autoflush(1);
	
	while ($client) {
		$outstream = "Hello World";
		print $outstream ."\r\n";
		print $client $outstream ."\r\n";
	   	sleep 1;
	}
    $client->close;
}
$server->close;
 
I use that kind of syntax to set up a timeout, but i don't know if it's the "good" way to do it. ( i post my code there is two or three day hopping someone will comment it, but... )

perhaps in you case something like this should work :
eval {
local $SIG{ALRM} = sub {die "TIMEOUT" } ;
alarm 10 ;
while($clients)
{
alarm 0 ;
print .....
alarm 10 ;
}
};
if ( $@ )
{
print "TIMEOUT ERROR after 10 secondes waiting !" ;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top