socketstcpreplystratum

TCP SOCKET - Stratum Server Reply - OpenEthereumPool


When I connect stratum server over /dev/tcp/host/port, I send json and got the right reply.

[water@Swan /tmp]$ exec 5<>/dev/tcp/127.0.0.1/8008;
[water@Swan /tmp]$ echo '{"jsonrpc":"2.0","id":1,"method":"eth_submitLogin","params":["0x00000a..27e"]}' >&5;
[water@Swan /tmp]$ cat <&5
{"id":1,"jsonrpc":"2.0","result":true}

However, when I try golang, python, PHP... any socket language, it won't work, the programme halts there. I tried many ways socket_recv($socket, $msg, 1000, MSG_DONTWAIT); or

 while (!feof($fp)){
    stream_set_timeout($fp, 1);
    echo fgets($fp, 1024);
}
    reply := make([]byte, 1024)

    _, err = conn.Read(reply)
    if err != nil {
        println("Write to server failed:", err.Error())
        os.Exit(1)
    }

    println("reply from server=", string(reply))

    conn.Close()

no of them seemed to work, but they all work on other TCP servers.

How can I get correct the Stratum TCP REPLY over the programme?

-- added full PHP

<?php

$a = array(
  "id" => 1,
  "jsonrpc" =>  "2.0",
  "method" => "eth_submitLogin",
  "params" => ["0x00000a006459...e"]
);

/*
$a = array(
"id" => 1, "jsonrpc" => "2.0", "method" => "eth_getWork"
);
*/

$b = json_encode($a);
$lb = strlen($b);
$h = "xk.com";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$conn = socket_connect($socket, $h, 8008);
socket_set_nonblock($socket);

var_dump(socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR));

$w = socket_write($socket, $b, $lb);
var_dump($socket, $conn, $w);

sleep(2);
socket_recv($socket, $msg, 1000, MSG_DONTWAIT);
$err = socket_last_error($socket);
var_dump(socket_strerror($err));
socket_close($socket);
var_dump($msg);

Solution

  • Your host appears to await a newline character after the JSON before it answers. If

    $b = json_encode($a)."\n";
    

    is used, your example works.

    Alternatively you could use

    socket_shutdown($socket, 1);
    

    after the socket_write() call if you have nothing more to send.