Advertisement

01.15.2007 at 06:02PM PST, ID: 22123758 | Points: 250
[x]
Attachment Details

How to access a UNIX domain socket bidirectionally

Asked by Neo-Rio in Perl Programming Language

I have a UNIX domain socket that is coded vaguely like this:-

$sockname="sock"
socket(UNIX, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!";
bind(UNIX, sockaddr_un($sockname)) || die "bind: $!";
listen(UNIX, SOMAXCONN) || die "listen: $!";
while (1)
{
    accept(C, UNIX);
    sysread(C, $_, 1024);

    s/^\s*//;
    my ($cmd, @args) = split('\s', $_);
    if (defined($cmds{$cmd}))
    {
                my $result = &{$cmds{$cmd}}(@args);
                syswrite(C, $result);
    }
    close(C);
}

and I am trying to write a client that can connect to it, send in specific commands, and the display the results that the socket sends back.
I can set up the connection to the socket OK, but I don't know how to send the server side commands properly, and nor do I know how to pick up any information that the server side sends back.

So far, I have come up with:-
#!/usr/bin/perl

use strict;
use Socket;
my ($sockname, $line);

$sockname = shift || 'sock';
socket(UNIX, PF_UNIX, SOCK_STREAM, 0) || die "socket :$!";
connect(UNIX, sockaddr_un($sockname)) || die "connect: $!";

** send command to sock and read response back **

close UNIX or die "close : $!";


How would I communicate bidirectionally with this socket?Start Free Trial
[+][-]01.15.2007 at 07:12PM PST, ID: 18321412

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.15.2007 at 07:22PM PST, ID: 18321442

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.28.2007 at 10:06PM PDT, ID: 19587085

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32