in reply to simple IPC needed

Well here is a Tk example to run the server. A few caveats...this is designed for a single connection only, otherwise you will need to incorporate IO::Select. I put a button, to save a log file, you can fill in the sub your self. You may even want to put a Tk repeat sub in there, to save a log every hour or so, and clear off the text buffer. Otherwise, the text widget will just start accumulating lines and growing in size.

You can also add a repeat sub to repeatedly check the latest time stamp in the text widget, and raise an alarm if one is missing.

#!/usr/bin/perl use strict; use warnings; use IO::Socket; use Tk; $|=1; $SIG{PIPE} = 'IGNORE'; my $listen = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 7070, Listen => 1, Reuse => 1, ) or die "Can't create listen socket : $!\n"; my $mw = MainWindow->new(); my $text = $mw->Scrolled('Text', -background =>'black', -foreground => 'yellow', )->pack(); my $subframe = $mw->Frame()->pack(); $subframe->Button(-text => 'Clear', -command => sub { $text->delete('1.0','end'); })->pack(-side=>'left'); $subframe->Button(-text => 'Save Log', -command => sub { })->pack(-side=>'left'); $subframe->Button(-text => 'Exit', -command => sub { exit })->pack(-side=>'right'); $mw->fileevent($listen, 'readable', sub { new_connection($listen) }); my $repeater = $mw->repeat(10000, sub{ $text->insert('end',"Checking stuff"; #do stuff here }); Tk::MainLoop; sub new_connection { my ($listen) = @_; my $client = $listen->accept() or warn "Can't accept connection"; $client->autoflush(1); $mw->fileevent($client, 'readable', sub { handle_connection($clien +t) }); #$client->print("Connected\n"); $text->insert('end', "Connected\t"); $text->see('end'); } sub handle_connection { my ($client) = @_; my $message = <$client>; if (defined $message and $message !~ /^quit/) { $message =~ s/[\r\n]+$//; #$client->print("Got message [$message]\n"); #echo back if wanted + $text->insert('end', "Got message [$message]\t"); $text->see('end'); } else { $text->insert('end', "Connection Closed\n"); $text->see('end'); $client->close(); } }

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: simple IPC needed
by redss (Monk) on Feb 19, 2005 at 04:45 UTC
    Thanks again for the excellent script. It works great in linux (kinda quirky in windows for some reason though).

    Regarding the previous script above of yours (with the deamonized server), how would I change the IP's if I want the client script to talk to the server script running on a different box? I couldn't get it working on different boxes...

      Just change the "localhost" to the numerical IP address that you want. For the server, set it to the ip address of the ethernet card, or whatever is exposed to the net, like 207.199.045.345. Do the same on the client. You may have firewall problems too. Whatever port you have opened, must be allowed to pass through the firewall, that goes for intranets too. A good firewall, also controls ports on the "local network".

      I'm not really a human, but I play one on earth. flash japh
        Ah, ok, so the server and client both need to be set to the IP of the server. Got it working. Thanks again for all your help!