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"; }

In reply to Re: Simple TCP Server to output data by pg
in thread Simple TCP Server to output data by gnu@perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.