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