From: Paul Kienzle <address@hidden>
To: "Corbin Champion" <address@hidden>
CC: Octave Help <address@hidden>
Subject: Re: using listen to receive commands
Date: Mon, 20 Mar 2006 21:09:30 -0500
The magic is pack("N",length($cmd)), plus some quoting hell:
#!/usr/bin/perl -w
# send.pl
# a simple client using IO:Socket
#----------------
use strict;
use IO::Socket;
my $host = shift || 'localhost';
my $port = shift || 2000;
my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
my $cmd = "fid = fopen('temp.txt','wb'); fprintf(fid,'this is perl\\n');
fclose(fid);";
print scalar(localtime);
print $sock "!!!x";
print $sock pack("N",length($cmd));
print $sock $cmd;
close $sock;
Also, if you use listen(2000,"debug") you will see every command received
in Octave.
- Paul
On Mar 20, 2006, at 8:03 PM, Corbin Champion wrote:
After reading that response, I think it is probably how I am sending the
command. To clearify...
It should send:
!!!x as a string?
the length of the command string, as a binary integer?
the command, as a string?
What I did would send each peice as a string which would obviously be
wrong after reading your response and looking back at the listen.txt.
sprintf outputs a string. I thought that might be wrong, but wasn't sure
how to have it directly write a binary integer to the socket. Wasn't in
my perl reference as well, so I will probably have to do some digging
there. After I get it so I am sending an 4-byte integer instead of a
string, I will worry about the byte order, which I am not sure of at this
point, but is a good question.
Thanks!
Corbin
From: Paul Kienzle <address@hidden>
To: "Corbin Champion" <address@hidden>
CC: address@hidden
Subject: Re: using listen to receive commands
Date: Mon, 20 Mar 2006 19:34:05 -0500
On Mar 20, 2006, at 12:55 PM, Corbin Champion wrote:
Are you familiar with perl? I am not familiar with tcl, but I have
taken a look at both files you pointed me at, and they seem to make
sense to help get me started. Based on looking at the eval function and
then converting to perl, I am trying to do a basic test of talking to
octave from perl. I have included the code here. The perl script is
able to connect to the octave that is listening "listen(2000)". It then
prints the !!!x format, is this done correctly?...probably not. Then I
see after the disconnect Afrom the socket "accept: no child processes"
printed out on the octave terminal that is listening. I know something
is wrong only by the fact that the file temp.txt was not created. What
should I expect to have printed out on the octave terminal as
connections are made and commands are sent?
Assuming sprintf('%b',60) produces a 4 byte integer, then what you have
looks correct. Is the integer in network byte order (big endian)? Or is
it an Intel little endian format?
Also, you should probably add fclose(fid); to your command. I don't know
what the behaviour on cygwin when terminating a process without closing
the associated files.
The "accept: no child processes" is a problem on some versions of Windows
that I don't understand. If you listen(2000,"nofork") then the problem
goes away (but you can only have one child listening at a time). Note
that this requires a newer version of listen.oct than that available on
the octave2.1.50a. I have a newer version available at
http://www.ncnr.nist.gov/reflpak/listen.oct
for the 2.1.50a version. The new windows package and the cygwin package
already support "nofork".
- Paul
#!/usr/bin/perl -w
# send.pl
# a simple client using IO:Socket
#----------------
use strict;
use IO::Socket;
my $host = shift || 'localhost';
my $port = shift || 2000;
my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
print scalar(localtime);
print $sock "!!!x";
print $sock sprintf("%b",60);
print $sock "fid = fopen('temp.txt', 'w'); fprintf(fid,'this is
perl\n');";
print scalar(localtime);
sleep(5);
close $sock;
Thanks for you help!
Corbin
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------