How is the usage of sockets in PHP?
In PHP, the sockets extension can be used to create and manage network socket connections. Here are common use cases for using the sockets extension:
- create a socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- bind the socket
socket_bind($socket, '127.0.0.1', 8000);
- Start listening on a socket
socket_listen($socket);
- Accept a socket connection
$newSocket = socket_accept($socket);
- write to a socket
- read from a socket
$message = 'Hello, World!';
socket_write($newSocket, $message, strlen($message));
$data = socket_read($newSocket, 1024);
- close the socket
socket_close($socket);
These are common use cases in the sockets extension, which can be adjusted and used accordingly based on specific requirements and socket types.