Dear people,

I am having a problem with writing to the client of a UNIX domain socket.
Ok, lets say I have a daemon, like this:

$sock = IO::Socket::UNIX -> new (Local  => UXSOCKADDR,
                                 Type   => SOCK_STREAM,
                                 Listen => 10) || exit 1;

while ($client = $sock -> accept ()) {
    print STDERR "Got new connection! ($client)\n";
    print $client "Hi, how are you?\n";
    while (defined ($from_client = <$client>)) {
        chomp ($from_client);
        print $client ">$from_client<\n";
    }
}

This works, but is incredibly crude. The program waits for input from the
client, and only then processes possible commands. Ok, so I did this:

$sock = IO::Socket::UNIX -> new (Local  => UXSOCKADDR,
                                 Type   => SOCK_STREAM,
                                 Listen => 10) || exit 1;
$sel = IO::Select -> new ($sock);

for (;;) {
    foreach $client ($sel -> can_read (0.1)) {
        if ($client == $sock) {
            $client = $sock -> accept ();
            $sel -> add ($client);
            nonblock ($client);
        } else {
            my $input;
            my $got = $client -> recv ($input, POSIX::BUFSIZ, 0);
            unless (defined ($got) && length ($input)) {
                disconnect ($client);
                next;
            }
            $inbuffer{$client} .= $input;
            push (@{$ready{$client}}, $2) while ($inbuffer{$client} =~
s/(<command>((.|\n)*?)<\/command>\n)//m);
        }
    }
    foreach $client (keys %ready) {
        while ($request = shift @{$ready{$client}}) {
            print $client ......
        }
        delete $ready{$client};
    }
}

This snippet of code iterates over the possible clients, and starts
processing commands until it receives a valid <command></command> pair from
its clients. So far so good. Except that I can no longer write to the
client! No matter what I do, the "print $client ......" does nothing.
Absolutely nothing gets parsed to the client. I did a ktrace on it, and it
reveals that no data of any kind gets parsed to the client.

I tried to do a send on it, like "send ($client, $buffer, $len, 0)" and
such, but nothing works. The only way I have been able to send something
back to the client, is after the "while (defined ($from_client = <$client>)"
loop in the first example, which is totally unacceptable.

Ideally, I want to check whether the client has input, not using the wait
"while (defined ($from_client = <$client>)" of the first example; that
happens in the second example, but there I can no longer write back to the
client!

Any suggestions would be highly appreciated,

- Mark


_______________________________________________
Perl-Unix-Users mailing list. To unsubscribe go to 
http://listserv.ActiveState.com/mailman/subscribe/perl-unix-users

Reply via email to