Socket in PHP

Server:

<?php
// server.php
// Remember to use double quote for \r\n

$ip = '127.0.0.1';
$port = '1337';

// Set up our socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\r\n";
}else{
    echo "socket_create() succeed.\n";
}

if (socket_bind($socket, $ip, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\r\n";
}else{
    echo "socket_bind() succeed.\r\n";
}

socket_listen($socket);
echo "socket_listen() succeed.\r\n";

//socket_set_nonblock($socket);

// Initialize the buffer
$buffer = "NO DATA";
while(true)
{
    // Accept any connections coming in on this socket
    echo "Accepting...\n";
    //System will be pending and waiting for connections
    $connection = socket_accept($socket);
    if ($connection === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
        break;
    }
    printf("Socket connected\r\n");

    //Client must use socket_read($connection, 1024, PHP_BINARY_READ) to receive multiple lines
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($connection, $msg, strlen($msg));

    // Check to see if there is anything in the buffer
    echo "buffer:" . $buffer . "\n";
    if($buffer != "")
    {
        //https://openhome.cc/Gossip/CGossip/PrintfScanf.html
        //基本上,printf() 就是將指定的文字、數值等輸出至螢幕上,並且執行過後會傳回所輸出的字元數
        printf("Something is in the buffer...sending data...\r\n");
        socket_write($connection, $buffer . "\r\n");
        printf("Wrote to socket\r\n");
    }
    else
    {
        printf("No Data in the buffer\r\n");
    }

    // Get the input
    /*
        PHP_NORMAL_READ: single line
        PHP_BINARY_READ: multiple line (Default)
    */
    while($data = socket_read($connection, 1024, PHP_NORMAL_READ))
    {
        $buffer = $data;
        socket_write($connection, "Information Received\r\n");
        printf("Buffer: " . $buffer . "\r\n");
    }

    socket_close($connection);
    printf("Closed the socket\r\n\r\n");
}
?>

Client:

<?php
// client.php
// Remember to use double quote for \r\n

$ip = '127.0.0.1';
$port = '1337';

// Create the socket and connect
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, $ip, $port);

while($buffer = socket_read($socket, 1024))
{
    //printf("Buffer:%s\n", $buffer);
    echo "buffer:" . $buffer . "\n";
    if($buffer == 'NO DATA')
    {
        echo('NO DATA');
        break;
    }
    else
    {
        // Do something with the data in the buffer
        echo("Buffer Data: " . $buffer . "");
        break;
    }
}


echo('Writing to Socket');

// Write some test data to our socket
if(!socket_write($socket, "SOME DATA\r\n"))
{
    echo('Write failed');
}

// Read any response from the socket
while($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)){
    echo("Data sent was: SOME DATA Response was:" . $buffer . "");
}
echo('Done Reading from Socket');

exit();
?>

Commands:

php server.php
php client.php

Reference:

  1. https://www.php.net/manual/en/function.socket-read.php
  2. https://xyz.cinc.biz/2016/12/php-socket-client.html
  3. socket_create() not found
    https://blog.johnsonlu.org/freebsdinstall-php-sockets-so/