Question

I was wondering if anyone has had any experience with this before. I'm trying to write a simple script that will continously read data from the TCP/IP stream but for some reason or another the script reads in a bunch of data, writes it out and then just stops.

$fp = fsockopen("xxxx", 3000, $errno, $errstr, 5);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    while (!feof($fp)) {
        echo fgets($fp, 128)."\n";
        fflush($fp);
    }
    fclose($fp);
}

I'd like it to have a constant flow to it, rather then echo out a bunch of data then wait 30 seconds and output a bunch more data. Anyone have any ideas?

---- EDIT ----

ZMQ Code

include 'zmsg.php';

$context = new ZMQContext();
$client = new ZMQSocket($context, ZMQ::SOCKET_DEALER);

// Generate printable identity for the client
$identity = sprintf ("%04X", rand(0, 0x10000));
$client->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $identity);
$client->connect("tcp://xxxx:3000");

$read = $write = array();
$poll = new ZMQPoll();
$poll->add($client, ZMQ::POLL_IN);

$request_nbr = 0;
while (true) {
    // Tick once per second, pulling in arriving messages
    for ($centitick = 0; $centitick < 100; $centitick++) {
        $events = $poll->poll($read, $write, 1000);
        $zmsg = new Zmsg($client);
        if ($events) {
            $zmsg->recv();
            echo $zmsg->body()."\n";
            //printf ("%s: %s%s", $identity, $zmsg->body(), PHP_EOL);
        }
    }
    $zmsg = new Zmsg($client);
    //$zmsg->body_fmt("request #%d", ++$request_nbr)->send();
}
Was it helpful?

Solution

Here is how you connect to a server (as a client) if your goal is ONLY to PULL data (read).

<?php
$context = new ZMQContext();
$sock = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$sock->connect("tcp://ADDRESS:3000");

while (true)
{
    $request = $sock->recv(); # recv is blocking by default, no need to put timers.
    printf ("Received: %s;%s", $request, PHP_EOL);
}
?>

if you want to reply, you'll need to use a pair socket (ZMQ::SOCKET_PAIR), then you can use:

$sock->send("data to send");

Also, if instead of you connecting to clients, clients connects to you, use the bind method instead of connect.

EDIT: use the PUSH socket type on the other side if you use the pull here, else, use the pair socket on both sides.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top