imafaz / esock
lightweight php library for simple socketing.
1.0.0
2024-07-13 16:13 UTC
Requires
- php-64bit: >=7.0
README
Esock is a lightweight library for create socket and read,write
Requirements
This library is supported by PHP versions 7.0 or higher
neili uses the socket php extention
Installation
The preferred way to install this extension is through Composer.
To install Esock, simply:
$ composer require imafaz/Esock
Quick Start
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php'; use Esock\Esock;
Usage
Create an instance of Esock
$esok = new Esock;
server side:
$ip = '91.107.153.188'; $port = 9000; $esock->initServer($ip, $port) or die($esock->lastError); while (true) { $client = $esock->accept() or die($esock->lastError); $input = $esock->read($client) or die($esock->lastError); echo $input; $esock->write($client, 'ok') or die($esock->lastError); $esock->close($client) or die($esock->lastError); }
client side:
$ip = '91.107.153.188'; $port = 9000; $esock = new Esock; $client = $esock->initClient($ip, $port) or die($esock->lastError); $esock->write($client, 'its a realy test') or die($esock->lastError); $input = $esock->read($client) or die($esock->lastError); echo $input;
Available Method
- initServer
// $esock->initServer($ip, $port); // example: $esock->initServer($ip, $port) or die($esock->lastError);
# Return (bool)
- initClient
// $esock->initClient($ip, $port); //example: $client = $esock->initClient($ip, $port) or die($esock->lastError);
# Return (bool)
- accept
// $esock->accept(); // example: $client = $esock->accept() or die($esock->lastError);
# Return (object|bool)
- read
// $esock->read(); // example: $input = $esock->read($clientSocket) or die($esock->lastError); echo $input;
# Return (string|bool)
- write
// $esock->write($clientSocket, 'its a realy test'); // example: $esock->write($client, 'ok') or die($esock->lastError);
# Return (bool)
- close
// $esock->close($socket); // example: $esock->close($socket);
# Return (bool)
License
- This script is licensed under the MIT License.