Location: PHPKode > scripts > Socket Server > socket-server/example.php
<?php

// Require SocketServer class
require_once 'SocketServer.php';


// Handler function
function my_handler($request)
{
    if (1 === preg_match('/quit|exit/i', $request)) {
        return null;
    }

    if (1 === preg_match('/stop|halt/i', $request)) {
        return false;
    }

    return md5($request) . PHP_EOL;
}


try {
    $server = new SocketServer(AF_INET, SOCK_STREAM, SOL_TCP);
    $server ->bind('0.0.0.0', 12345)
            ->setHandler('my_handler')
            ->run();
    exit(0);
} catch (Exception $e) {
    fwrite(STDERR, $e->getMessage() . PHP_EOL);
    exit(1);
}

Return current item: Socket Server