PHP code example of icicleio / socket

1. Go to this page and download the library: Download icicleio/socket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

icicleio / socket example snippets


use Icicle\Coroutine\Coroutine;
use Icicle\Loop;
use Icicle\Socket\Server\DefaultServerFactory;
use Icicle\Socket\Server\Server;
use Icicle\Socket\Socket;

$server = (new DefaultServerFactory())->create('localhost', 8080);

$generator = function (Server $server) {
    printf("Server listening on %s:%d\n", $server->getAddress(), $server->getPort());

    $generator = function (Socket $socket) {
        $request = '';
        do {
            $request .= (yield $socket->read(0, "\n"));
        } while (substr($request, -4) !== "\r\n\r\n");

        $message = sprintf("Received the following request:\r\n\r\n%s", $request);

        $data  = "HTTP/1.1 200 OK\r\n";
        $data .= "Content-Type: text/plain\r\n";
        $data .= sprintf("Content-Length: %d\r\n", strlen($message));
        $data .= "Connection: close\r\n";
        $data .= "\r\n";
        $data .= $message;

        yield $socket->write($data);

        $socket->close();
    };

    while ($server->isOpen()) {
        // Handle client in a separate coroutine so this coroutine is not blocked.
        $coroutine = new Coroutine($generator(yield $server->accept()));
        $coroutine->done(null, function ($exception) {
            printf("Client error: %s\n", $exception->getMessage());
        });
    }
};

$coroutine = new Coroutine($generator($server));
$coroutine->done();

Loop\run();

ClassOrInterfaceName::methodName(ArgumentType $arg): ReturnType

$server = new BasicServer(resource $socket)

Server::accept(): Generator

Server::getAddress(): string

Server::getPort(): int

ServerFactory::create(
    string $host,
    int $port = null,
    mixed[] $options = []
): Server

$socket = new NetworkSocket(resource $socket)

Socket::enableCrypto(int $method, float $timeout = 0): \Generator

Socket::isCryptoEnabled(): bool

Socket::unshift(string $data): void

Socket::getLocalAddress(): string

Socket::getLocalPort(): int

Socket::getRemoteAddress(): string

Socket::getRemotePort(): int

Connector::connect(
    string $host,
    int|null $port,
    mixed[] $options = []
): \Generator

$datagram = new BasicDatagram(resource $socket)

Datagram::receive(int $length, float $timeout): Generator

Datagram::send(
    string $address,
    int $port,
    string $data
): \Generator

Datagram::getAddress(): string

Datagram::getPort(): int

DatagramFactory::create(
    string $host,
    int $port = null,
    mixed[] $options = []
): Datagram

Icicle\Socket\connect(
    string $ip,
    int|null $port,
    array $options = []
): \Generator

Icicle\Socket\connector(
    Connector|null $connector = null
): Connector