[PHP] SocksWork - PHP Socket client library

09/07/2014 17:06 manulaiko3.0#1
Hi!

Yesterday night I've made a library to work with sockets, it's called SocksWork.

You can download it from here:
[Only registered and activated users can see links. Click Here To Register...]

Example code:
Code:
<?php
/**
 * SocksWork example file
 * 
 * This file is made in order to help you understand how SocksWork works
 * First we will instance a SocksWork object and a PacketBuilder object
 * We will use PacketBuilder object to build the packet that will be sent to the server
 * The packet will be like this:
 *         packetID: 1
 *         packetContent: Hello SocksWork!
 * The bytes of packets would be something like this:
 *       PACKET ID  | HELLO SOCKSWORK!
 *    00 00 00 01 00 00 00 16 72 101 108 108 111 32 83 111 99 107 115 87 111 114 107 33
 * The first 4 bytes are packetID:
 *         00 00 00 01 = 1
 * The rest of the packet is the content
 * As the content is just an string first 4 bytes indicates string length:
 *         00 00 00 16 = 16
 * And next bytes are string bytes:
 *         72 101 108 108 111 32 83 111 99 107 115 87 111 114 107 33 = Hello SocksWork!
 * 
 * Once we've sent the packet we will wait for the response
 * Let's assume that the response packet is something like this
 *         packetID = 2
 *         packetContent: Hello!
 * The bytes would be something like this:
 *         PACKET ID  | HELLO!
 *         00 00 00 02 00 00 00 06 72 101 108 108 111 33
 * First 4 bytes are packetID:
 *         00 00 00 02 = 2
 * Next is packet content
 * and as it is just a string we do it like before
 *         00 00 00 06 = 6 (string length)
 *        72 101 108 108 111 33 = Hello!
 * 
 * Ok, let's start!
 */
//Include SocksWork files
require_once '../SocksWork.php';
require_once '../SocksWork/PacketBuilder.php';

//Instance objects
$SocksWork = new SocksWork("localhost", 8080);
$PacketBuilder = new PacketBuilder(1);

//Establish a connection with the server
$SocksWork->connect();

//Build packet
$PacketBuilder->writeStr("Hello SocksWork!");

//Send packet
$SocksWork->send($PacketBuilder);

//Parse response
$PacketBuilder->readStr();

//Get response
$array = $PacketBuilder->getReadBytes();

var_dump($array);

/**
 * It will print something like this:
 * 
 * $array = Array(
 *         [0] => 2;
 *         [1] => "Hello!";
 * )
 * 
 * First index is received packetID
 * and second index is string
 */
Please let me know any kind of bug, I couldn't test it so hope it works.

Manulaiko.