imafaz/esock

lightweight php library for simple socketing.

1.0.0 2024-07-13 16:13 UTC

This package is auto-updated.

Last update: 2025-06-13 18:25:19 UTC


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);
Atttribute Description Type Required Default
$ip server listening ip string yes null
$port server listening port int yes null

# Return (bool)

- initClient

// $esock->initClient($ip, $port);

//example:

$client = $esock->initClient($ip, $port) or die($esock->lastError);
Atttribute Description Type Required Default
$ip server ip string yes null
$port server port int yes null

# 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;
Atttribute Description Type Required Default
$clientSocket client accept socket object yes null

# Return (string|bool)

- write

// $esock->write($clientSocket, 'its a realy test');

// example:

$esock->write($client, 'ok') or die($esock->lastError);
Atttribute Description Type Required Default
$clientSocket client accept socket object yes null
$output text to write string yes null

# Return (bool)

- close

// $esock->close($socket);

// example:

$esock->close($socket);
Atttribute Description Type Required Default
$socket socket object object yes null

# Return (bool)

License