Page 1 of 1

PHP Socket Noob Problem

Posted: Tue Mar 30, 2010 11:17 am
by AlCapwn
I have an existing python script that uses sockets to communicate with a game server and I am trying to recreate some of the functionality in PHP. I have a series of functions that manually packs a binary string according to the standards set by the games rcon protocol. From then I pass the data to the socket and try to send it and receive a response. However, the response is always empty.

http://pastebin.org/128684
Is this wrong?

My first guess was that the string I pass to socket_send() is supposed to be ASCII, and then it converts it when it sends it which means its trying to re-convert my data to binary... but I couldn't find any solid evidence of that.

Any help would be greatly appreciated

Re: PHP Socket Noob Problem

Posted: Tue Mar 30, 2010 11:33 am
by Weirdan
My first guess was that the string I pass to socket_send() is supposed to be ASCII, and then it converts it when it sends it which means its trying to re-convert my data to binary...
Your guess is wrong, php does not have a binary string/buf type and uses strings instead. Strings are interpreted as an array of bytes where necessary. It's binary safe.

Re: PHP Socket Noob Problem

Posted: Tue Mar 30, 2010 11:38 am
by Weirdan
Works for me:

Code: Select all

<?php
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_bind($socket, '127.0.0.1');
    socket_connect($socket, 'localhost', 22);
    socket_set_block($socket);

    $request = 'qweqwe';

    socket_send($socket, $request, 4096, 0);

    socket_recv($socket, $response, 4096, 0);

    echo "Response: $response"; // Response: SSH-2.0-OpenSSH_5.3p1 Debian-3

Re: PHP Socket Noob Problem

Posted: Tue Mar 30, 2010 12:16 pm
by AlCapwn
Well something is getting messed up along the way and I assume its in the sending.

I printed out my packet as hex in both PHP (bin2hex) and Python (hexlify) so that I could compare to ensure I am trying to send the same data and I have:
000000001d000000010000000c0000006c6f67696e2e68617368656400
And it matches in both programs.

That binary string is stored in $getPasswordSaltRequest and being passed to the socket_send() function. So I must have the socket set up wrong or something because the data being sent is correct.

This is how the socket was created in python:

Code: Select all

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serverSocket.connect( ( host, port ) )
serverSocket.setblocking(1)

getPasswordSaltRequest = EncodeClientRequest( [ "login.hashed" ] )
serverSocket.send(getPasswordSaltRequest)
And this is how I did it in PHP:

Code: Select all

set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $myIP);
socket_connect($socket, $host, $port);
socket_set_block($socket);

$getPasswordSaltRequest = EncodeClientRequest(array("login.hashed"));
socket_send($socket, $getPasswordSaltRequest, 4096, 0);
socket_recv($socket, $getPasswordSaltResponse, 4096, 0);
So I mean, it looks like its set up correctly. I don't really understand the meaning of the flag parameter for socket_send() and socket_recv() though, so that may be my problem. No matter what, $getPasswordSaltResponse is always empty.