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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |