<p>I was sending the data across the socket in hexadecimal format, but it appears that it should be in binary.</p>

<p>This does the trick: <code>hex2bin($message)</code></p>

<p>Link to just-about-working script: <a href="https://bitcoin.stackexchange.com/a/48399/24926">https://bitcoin.stackexchange.com/a/48399/24926</a></p>

CC BY-SA 3.0
2016-09-03T15:35:24.070Z
2016-09-03T18:39:56.370Z
2017-04-13T12:47:33.283Z
inersha
<p>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.</p>

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

<h2>1. Version Message</h2>

<p>I have contstructed my own version message. The result looks like this:</p>

<pre><code>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
</code></pre>

<p>Are there any obvious mistakes? Things I'm unsure of are:</p>

<ul>
<li>What is my addr_from port meant to be?</li>
</ul>

<h2>2. Socket Connection</h2>

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

<pre><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);
</code></pre>

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

<p><em>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.</em> Can you point me in the right direction?</p>

<hr>

<p>Here's my PHP script so far: <a href="http://pastebin.com/q7zf4NMj" rel="nofollow">http://pastebin.com/q7zf4NMj</a></p>

<network>
How to connect to a node using PHP
inersha
# How to connect to a node using PHP

(Asked by: username_0 on 2016-09-03)

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>

**Tags:** network


---

## 1 answers

### Answer by username_0 on 2016-09-03. Score: 1 | Accepted answer

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>


---

