imafaz/esock

轻量级的PHP库,用于简单的套接字操作。

1.0.0 2024-07-13 16:13 UTC

This package is auto-updated.

Last update: 2024-09-13 16:28:56 UTC


README

Esock是一个轻量级的库,用于创建套接字和读写。

需求

此库支持PHP版本7.0或更高版本
neili使用了socket PHP扩展

安装

安装此扩展的首选方式是通过Composer

要安装Esock,只需

$ composer require imafaz/Esock

快速开始

使用Composer使用此库

require __DIR__ . '/vendor/autoload.php';

use Esock\Esock;

用法

创建Esock实例

$esok = new Esock;

服务器端

$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);
}

客户端

$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;

可用方法

- initServer

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

// example:

$esock->initServer($ip, $port) or die($esock->lastError);

返回 (bool)

- initClient

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

//example:

$client = $esock->initClient($ip, $port) or die($esock->lastError);

返回 (bool)

- accept

// $esock->accept();

// example:
$client = $esock->accept() or die($esock->lastError);

返回 (object|bool)

- read

// $esock->read();

// example:
$input = $esock->read($clientSocket) or die($esock->lastError);

echo $input;

返回 (string|bool)

- write

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

// example:

$esock->write($client, 'ok') or die($esock->lastError);

返回 (bool)

- close

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

// example:

$esock->close($socket);

返回 (bool)

许可