How to connect to a node using PHP I want to make a simple PHP script that connects to a node (on my server) so that I can receive the data it relays.

However, I am new to sockets, and I am having difficulty with the handshake process. This is what I've got so far:

1. Version Message

I have contstructed my own version message. The result looks like this:

Config
------
version:      60002
node:         85.119.83.25:8333
local:        90.213.66.225:8333
start_height: 0


VERSION MESSAGE
---------------
Header:Array
(
    [magicbytes] =&gt; F9 BE B4 D9
    [command] =&gt; 76 65 72 73 69 6F 6E 00 00 00 00 00
    [payload_size] =&gt; 55 00 00 00
    [checksum] =&gt; 18 32 C2 E4
)

Payload:Array
(
    [version] =&gt; 62 EA 00 00
    [services] =&gt; 01 00 00 00 00 00 00 00
    [timestamp] =&gt; 18 A3 CA 57 00 00 00 00
    [addr_recv] =&gt; 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 55 77 53 19 20 8D
    [addr_from] =&gt; 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 5A D5 42 E1 20 8D
    [nonce] =&gt; 87 58 8D 22 00 00 00 00
    [user_agent] =&gt; 00
    [start_height] =&gt; 00 00 00 00
)

Serialized:
F9BEB4D976657273696F6E0000000000550000001832C2E462EA0000010000000000000018A3CA5700000000010000000000000000000000000000000000FFFF55775319208D010000000000000000000000000000000000FFFF5AD542E1208D87588D22000000000000000000


Are there any obvious mistakes? Things I'm unsure of are:


What is my addr_from port meant to be?


2. Socket Connection

Next I'm trying to send this version message to my node, and get a version message back. Here is my code:

$socket = socket_create(AF_INET, SOCK_STREAM, 6);
socket_connect($socket, $node_ip, $node_port);
socket_send($socket, $message, strlen($message) / 2, 0);
socket_recv($socket, $buf, 2000, MSG_WAITALL);

var_dump($buf);


All of the socket functions appear to be successful, but the result of var_dump($buf) is NULL.

I suppose my biggest problem is that I'm not receiving any error messages, so it's difficult to troubleshoot where I'm going wrong. Can you point me in the right direction?



Here's my PHP script so far: http://pastebin.com/q7zf4NMj
I was sending the data across the socket in hexadecimal format, but it appears that it should be in binary.

This does the trick: hex2bin($message)

Link to just-about-working script: https://bitcoin.stackexchange.com/a/48399/24926