2

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] => F9 BE B4 D9
    [command] => 76 65 72 73 69 6F 6E 00 00 00 00 00
    [payload_size] => 55 00 00 00
    [checksum] => 18 32 C2 E4
)

Payload:Array
(
    [version] => 62 EA 00 00
    [services] => 01 00 00 00 00 00 00 00
    [timestamp] => 18 A3 CA 57 00 00 00 00
    [addr_recv] => 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] => 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] => 87 58 8D 22 00 00 00 00
    [user_agent] => 00
    [start_height] => 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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.