rhxk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I have a script that listens on the UNIXDGRAM socket for connections & have multiple scripts that call this single listening process. If two or more scripts call this listening process, would they clash? If so, how can I get out of it? Any suggestions?
use MY::Variables; use Socket; $addr = sockaddr_un($LogSocket); socket(SERVER, PF_UNIX, SOCK_DGRAM, 0) || die "Could not create socket +: $!"; unlink("$LogSocket"); bind(SERVER, $addr) || die "Could not bind: $!"; #select((select(SERVER), $| = 1)[0]); #enable command buffering while (1) { $x = recv(SERVER,$msg,1024,0); if (defined($x)) { my ($time,$level,$Program,$message,@sites) = split(chr(28),$ms +g); foreach my $site (sort @sites) { my $email = "$time [$Program] $message"; open(LOG, ">> $GnrtLog/$site.log") || die "cannot open $Lo +gSocket $!"; print LOG $email; close(LOG); } } else { print "hi"; $count++; }
If no process is sending something to this listening process (above)
then the above script idles on recv line until it receives something again? How can I make it get out of the 'recv' cond to exec the count++?

The multiple clients:
use MY::Variables; use Socket; <some irrelevant stuff here> if (socket(SERVER, PF_UNIX, SOCK_DGRAM, 0)) { my $send = send(SERVER, $message, 0, sockaddr_un($LogSocke +t)); if (defined($send)) { print "sent $message"; } } else { print "I couldn't connect $!"; } close(SERVER);


thanx for any suggestions... (let me know if I'm being unclear)

Replies are listed 'Best First'.
Re: clashing sockets?
by ikegami (Patriarch) on Sep 07, 2004 at 22:42 UTC

    [Answer partially removed. I was thinking INET, not unix domain sockets]

    If no process is sending something to this listening process (above) then the above script idles on recv line until it receives something again? How can I make it get out of the 'recv' cond to exec the count++?

    Use select() to wait for data to become available on the socket. select() can be given a timeout so you can do your idle processing.

      clashing in a sense of:
    • there's only 1 script that does the listening
    • and 2 or more clients simultaneously send a query to the listening process.
      would the script that listens be able to capture both of queries or 1 or none?


    • oh, by select you mean select((select(SERVER), $| = 1)[0]); ?

        Shoot, while trying to figure out what sockaddr_un does, I realise these are unix sockets! I can't help you with the clashing questions.

        And no, the other select in perlfunc: select RBITS,WBITS,EBITS,TIMEOUT, or its OO interface, IO::Select