in reply to Simple TCP Server to output data

It is not part of the TCP standard, to check whether the peer is still alive, or whether the connection is still there. The TCP conncections are only maintained "locally".

connected() can not be used to check the availability of peer, or the status of connection.

However for Perl socket programs, when the peer closes the connection, you would reach eof of the socket, just as a normal file does.

This demo shows how the socket reaches eof, when yahoo closes the connection after the transmission is done:
use strict; use IO::Socket; my $yahoo = IO::Socket::INET->new(Proto => "tcp", PeerPort => 80, PeerAddr => "www.yahoo.com", Timeout => 2000) || die "failed to connect\n"; my $req = "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n"; print $yahoo $req; my $count = 0; while (<$yahoo>) { print "packet count = ", ++ $count, ", and first 20 chars of the p +acket = ", substr($_, 0, 20), "\n"; }

Replies are listed 'Best First'.
Re: Re: Simple TCP Server to output data
by gnu@perl (Pilgrim) on Mar 18, 2003 at 13:50 UTC
    Thanks for your help, but what you have shown is a client, I am writing a server. The problem I have is knowing when the client has disconnected from me.

    I am thinking that the only real way for me to know is to write a null to the client before I do the actual write, if that works the client is probably still there, otherwise it has disconnected.