use IO::Socket::INET; $SIG{INT} = \&endit; #ctrl+c $SIG{QUIT} = \&abandon; #ctrl+\ $run=1; # auto-flush on socket $| = 1; # create a connecting socket our $socket = new IO::Socket::INET ( PeerHost => '127.0.0.1', PeerPort => '9001', Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; print "connected to the server\n"; # data to send to a server #my $req = 'hello world'; #my $size = $socket->send($req); #print "sent data of length $size\n"; # notify server that request has been sent (I have stopped writing data) #shutdown($socket, 1); # receive a response of up to 1024 characters from server my $response = ""; $socket->recv($response, 1500); print "received response: $response\n"; if ($response =~ /^STATE=STOP\n/) { print "starting\n"; $socket->send("RUN\n"); } elsif ($response =~ /^STATE=RUN\n/) { print "Already running\n"; } while($run) { $socket->recv($response, 1500); if ($response ne "\0") { print "$response"; } } print "stopping data\n"; $socket->send("STOP\n"); select(undef, undef, undef, 0.5);#500ms delay $socket->close(); sub endit() { print "\nhalting rx only\n"; exit(); } sub abandon() { $run=0; }