G
Gavin Hamill
Hullo - I'm trying to communicate with our office PBX, and whilst I have the
full protocol documentation, I'm failing on basic socket programming...
I've ripped most of an example from the Perl Cookbook and modified it so I
have a basic interactive client that works fine:
#!/usr/bin/perl
# Message format is 32-bit little-endian length followed by
# the message, terminated with 0x0d0a incoming and 0x0d outgoing.
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => '10.0.0.246',
PeerPort => 4000,
Proto => 'tcp',
);
die "Socket could not be created. Reason: $!\n" unless $sock;
$sock->autoflush(1);
print STDERR "[Connected]\n";
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());
if ($kidpid) {
while (sysread($sock, $header,4) == 4) {
# We got a 4-byte header!
$paylen= unpack("V",$header);
print "Got message length of $paylen payload bytes\n";
if(sysread($sock, $payload, $paylen) == $paylen) {
print $payload;
}
else {
print "ERR! Bytes read didn't match value in header - received this:
-=$payload=-\n";
}
}
kill("TERM" => $kidpid); # send SIGTERM to child
}
else {
# Magic login sequence
print $sock "\x02\x00\x00\x00\x87\x00";
while (defined ($line = <STDIN>)) {
chop $line; # Remove the 0x0a LF
print $sock pack("V",length($line)+1);
print $sock $line . "\x0d"; # Suffix a CR
}
}
exit;
My problem is this:
The PBX will be sending me events, and I will need to send responses to the
PBX, parse the responses and perhaps send different replies...
If this script has fork()ed ... how on earth do I get the two halves to talk
to each other? named pipes? UNIX domain sockets?
After much reading I've drawn a blank - does anyone have suggestions or some
short code fragments I could work from?
Cheers,
Gavin,.
full protocol documentation, I'm failing on basic socket programming...
I've ripped most of an example from the Perl Cookbook and modified it so I
have a basic interactive client that works fine:
#!/usr/bin/perl
# Message format is 32-bit little-endian length followed by
# the message, terminated with 0x0d0a incoming and 0x0d outgoing.
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => '10.0.0.246',
PeerPort => 4000,
Proto => 'tcp',
);
die "Socket could not be created. Reason: $!\n" unless $sock;
$sock->autoflush(1);
print STDERR "[Connected]\n";
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());
if ($kidpid) {
while (sysread($sock, $header,4) == 4) {
# We got a 4-byte header!
$paylen= unpack("V",$header);
print "Got message length of $paylen payload bytes\n";
if(sysread($sock, $payload, $paylen) == $paylen) {
print $payload;
}
else {
print "ERR! Bytes read didn't match value in header - received this:
-=$payload=-\n";
}
}
kill("TERM" => $kidpid); # send SIGTERM to child
}
else {
# Magic login sequence
print $sock "\x02\x00\x00\x00\x87\x00";
while (defined ($line = <STDIN>)) {
chop $line; # Remove the 0x0a LF
print $sock pack("V",length($line)+1);
print $sock $line . "\x0d"; # Suffix a CR
}
}
exit;
My problem is this:
The PBX will be sending me events, and I will need to send responses to the
PBX, parse the responses and perhaps send different replies...
If this script has fork()ed ... how on earth do I get the two halves to talk
to each other? named pipes? UNIX domain sockets?
After much reading I've drawn a blank - does anyone have suggestions or some
short code fragments I could work from?
Cheers,
Gavin,.