I have a simple program that accepts a connection on the network then opens a named pipe to read data from and send that data to the connectet client.

For the most part it works fine. I have one problem though. I don't know how to tell, in a loop, if the client is still connected. This causes a problem when a client disconnects. The next read of the pipe is still pending and the state of the loop thinks the client is still there so it prints the next data it receives to the socket.

On the next iteration I again check to make sure the client is there and if it isn't I go back into accept(). With this model, I lose 1 loops worth of data each time a clients disconnects.

Any Ideas? I'm stumped. Anyway, here is the code and thanks in advance:

#!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; my $pipefile = shift || "./genpipe"; my $serverPort = shift || 4509; syswrite(STDOUT,"Before listenSocket\n"); my $listenSocket = IO::Socket::INET->new(LocalPort => $serverPort, Listen => 50, Proto => 'tcp', Reuse => 1, timeout => 60*60, ); die "Cannot set up listening socket!\n" unless $listenSocket; while (1) { my $incoming = $listenSocket->accept(); $incoming->autoflush(1); syswrite(STDOUT,"ACCPETED -> $incoming\n"); while ($incoming->connected()) { open(PIPE,"<$pipefile") or die "Cannot open pipe $pipefile. $ +!\n"; if (! $incoming->connected() ) # The above line will always be the same # as the initial check before the loop, so # I never get to the code block just below, # and I lose the data on this iteration. { close(PIPE); syswrite(STDOUT,"EXECUTING 'last' STATEMENT!\n"); last; } syswrite(STDOUT,"OPENED PIPE\n"); syswrite(STDOUT,"** just before reading pipe\n"); syswrite(STDOUT,"just before write to channel\n"); map {syswrite($incoming,$_)} (<PIPE>); syswrite(STDOUT,"just after write to channel\n"); close(PIPE); } }

In reply to 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.