[akropel1@rochester.rr.com: Re: Reading NIS using fsockopen in php]

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[akropel1@rochester.rr.com: Re: Reading NIS using fsockopen in php]

Adam Kropelin
On Sun, Sep 11, 2005 at 02:32:16PM +0200, Arjen Korevaar wrote:

> Thank you for your help so far. I guessed it wouldn't be difficult
> to rewrite some of the c code to php, as these languages have
> basically the same origin and look a lot like eachother.
> I don't know if you're familiar with the php programming language,
> but I'd like you to take a look at my code-so-far one more time...
> <?
> $host = "";
> $port = "3551";
> $msg = "status";
> $buf = "test"; // just to see if it has changed after reading
> $socket = socket_create(AF_INET, SOCK_STREAM, 0);
> if (socket_connect($socket, $host, $port) > 0) {
> socket_write($socket, strlen($msg) . "\0", strlen(strlen($msg)));
That bit doesn't make sense. You're taking a length (an integer) and
appending an ASCII NUL onto it. I assume what you're trying to do is
send the 16 bit length. First, you need to send it as raw binary, not
ASCII. Second, it needs to be exactly 16 bits (two bytes); your code is
not ensuring that.

You want something like this:

        $len = pack("n", strlen($msg));
        socket_write($socket, $len, 2);

> socket_write($socket, $msg . "\0", strlen($msg));

Get rid of the "\0", otherwise that code is correct.

> socket_recv($socket, $buf, 1024, 0);

Now you want to first read a 16 bit length:

        socket_recv($socket, $buf, 2, 0);
        $len = unpack("nlen", $buf)["len"];

Now read the number chars specified by the length:

        socket_recv($socket, $buf, $len);

Note this code isn't actually tested, but you should get the idea. Look
up the definitions of pack() and unpack() in the php docs to see how
they work.


SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
Apcupsd-users mailing list
[hidden email]