in reply to simple IPC needed

Here is about as simple a client server setup, as you can get. I made the server a daemon, but in your case you may want to separate the 2, and have a server-client connection going, and have a separate daemon watching the file. The client disconnects after each write here, but I put it in a while loop to test.(Just remove the while loop in the client for single writes.) Also check out Proc::Daemon for probably a better way to daemonize, I did it manually here.
########## server #################################### #!/usr/bin/perl use warnings; use strict; use POSIX 'setsid'; use IO::Socket; $|++; my $server = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7070', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $server; $server->autoflush(1); daemonize(); open(LOG,">/tmp/7070.log") or die "$\n"; while(1){ while ( my $client = $server->accept() ){ sysread($client, my $buf, 100); syswrite(LOG, "$buf\n"); LOG->flush; } } ################################################################# sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; } __END__ ################################# ################################# ################################# ############ client ############################# #!/usr/bin/perl use warnings; use strict; use IO::Socket; $|++; while(1){ my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '7070', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; $sock->autoflush(1); # Send a transmission print $sock time,"\n"; print time,"\n"; close ($sock); sleep(1); } exit 0;

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 18, 2005 at 18:37 UTC
    Uh oh, unexpected problem here...

    I can't merge your excellent IO::Socket code with my code that uses Tk, can I?

    Because your example loops waiting for client messages, whereas my current code using Tk needs to go off to Tk::MainLoop, right?

    Maybe for this application, it would be better for me to get the Tk send method to work (but I am stuck at the moment; see my reply above)

    hmmmmmm.....any ideas?

      Oh Yeah, you can use sockets with Tk, you just need to use fileevent to listen. I'll whip up a little demo, and post in later today.

      I'm not really a human, but I play one on earth. flash japh
Re^2: simple IPC needed
by redss (Monk) on Feb 18, 2005 at 18:18 UTC
    Well that example worked perfectly right off the bat - and it didn't require Tk or any modules that I didn't have preinstalled already.

    Great work - thanks!!!