vadnica-logo
X

socket_set_blocking()

The socket_set_blocking() function (an alias for stream_set_blocking()) determines whether a socket operation (such as reading or writing) will stop script execution until it is completed, or return immediately.

server.php

<?php
$host = "127.0.0.1";
$port = 8080;

// Create a TCP/IP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    die("Could not create socket: " . socket_strerror(socket_last_error()));
}

// Connect socket to localhost:8080
if (socket_bind($socket, $host, $port) === false) {
    die("Could not bind socket: " . socket_strerror(socket_last_error($socket)));
}

// Start listening on the socket
if (socket_listen($socket, 5) === false) {
    die("Could not listen on socket: " . socket_strerror(socket_last_error($socket)));
}

echo "Server is listening on $host:$port\n";

do {
    $client = socket_accept($socket);
    if ($client !== false) {
        $message = "Welcome to my PHP tutorial!\n";
        socket_write($client, $message, strlen($message));
        socket_close($client);
    }
} while (true);

socket_close($socket);
?>

client.php

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    die("Could not create socket: " . socket_strerror(socket_last_error()));
}

// Connect to server
$result = socket_connect($socket, "127.0.0.1", 8080);
if ($result === false) {
    die("Could not connect: " . socket_strerror(socket_last_error($socket)));
}

$input = "";
while ($out = socket_read($socket, 2048)) {
    $input .= $out;
}
if ($input) {
    echo "Received data: $input\n";
} else {
    echo "No data received.\n";
}

// Close the socket
socket_close($socket);
?>

First, let's run the server.php script in the terminal (CMD on Windows). The server will listen on 127.0.0.1:8080. Then, run the client.php script in a second terminal or browser. The client will attempt to connect to this server. In CMD or the terminal, you must first navigate to the folder containing the file (using the cd folder_name command). Once in the correct folder, start the PHP server with the command:

php server.php

If the terminal returns an error stating that the 'php' command is not recognized, it means PHP is not in the system PATH. The error looks like this:

php : The term 'php' is not recognized...

In this case, you must tell the terminal exactly where the php.exe program is located and which file it should run. If your files are in the C:\projectName\ folder and XAMPP is in the C:\xampp\ folder, construct the command in the terminal as follows:

C:\projectName\ C:\xampp\php\php.exe server.php

After pressing Enter, the following is displayed:

Server is listening on 127.0.0.1:8080

When this is displayed, run client.php in a second terminal window in the same way:

Received data: Welcome to my PHP tutorial!

Explanation

Notes:

Using non-blocking mode is useful when building event-driven applications where we do not want a single operation to stop the entire application.

Running on Linux (LAMPP)

If you are using Linux and have XAMPP (LAMPP) installed, the path to PHP is different. Open the terminal, navigate to your project folder, and use the following command:

/opt/lampp/bin/php server.php

In Linux, the terminal usually already knows where PHP is located if you have added it to your PATH, so php server.php is often sufficient. Don't forget about execution permissions if you were to run the script directly.

Thank you for visiting! Adding privacy policy.

© 2024 All rights reserved.

Vam je koda pomagala? Če želite podpreti moj trud pri pripravi vodičev in vzdrževanju strani, mi lahko namenite donacijo za kavo.