Here’s an overview of the PHP socket functions:
socket_create()
This function is used to create a socket resource that represents a network socket.
It accepts three parameters: the address (Ex: AF_INET for IPv4), the socket type (Ex: SOCK_STREAM for TCP), and the protocol (Ex: SOL_TCP for TCP).
It returns a socket resource on success or false
on failure.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind()
The socket_bind() PHP function is used to bind a socket to a specific address and a port.
It accepts the socket resource as the first parameter, followed by the IP address and port number.
When a socket is bound to a certain address, it can listen for incoming connections on that address.
// Bind the socket to a specific address and port
$bindResult = socket_bind($socket, '127.0.0.1', 8080);
if ($bindResult === false) {
// Handle bind failure
echo "Failed to bind: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Socket successfully bound
// Perform further operations (e.g., listen, accept connections)
}
socket_connect()
In PHP, the socket_connect() function is used to connect to a remote socket. It is most commonly used in socket programming to establish a connection to a remote server or socket endpoint.
// Connect to a remote server
$result = socket_connect($socket, '127.0.0.1', 8080);
if ($result === false) {
// Handle connection failure
echo "Failed to connect: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Connection successful
// Use the socket for further operations (e.g., send, receive)
}
socket_listen()
The socket_listen() function in PHP is used to make a socket listen for incoming connections. It configures a bound socket to accept connections from clients.
// Bind the socket to a specific address and port
socket_bind($socket, '127.0.0.1', 8080);
// Listen for incoming connections
$listenResult = socket_listen($socket, 5);
if ($listenResult === false) {
// Handle listen failure
echo "Failed to listen: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Socket is now listening for connections
// Accept incoming connections or perform other operations
}
socket_accept()
The socket_accept() function in PHP is used to accept an incoming connection on a listening socket. It creates a new socket resource dedicated to the specific client connection.
// Bind the socket to a specific address and port
socket_bind($socket, '127.0.0.1', 8080);
// Listen for incoming connections
socket_listen($socket, 5);
// Accept an incoming connection
$clientSocket = socket_accept($socket);
if ($clientSocket === false) {
// Handle accept failure
echo "Failed to accept connection: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Connection accepted
// Use $clientSocket for further communication with the client
}
// Close the client socket
socket_close($clientSocket);
socket_read()
The socket_read() function in PHP is used to read data from a socket. It allows you to receive data sent from the remote endpoint through the established connection.
// Connect to a remote server
socket_connect($socket, '127.0.0.1', 8080);
// Read data from the socket
$data = socket_read($socket, 1024);
if ($data === false) {
// Handle read failure
echo "Failed to read from socket: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Data read successfully
echo "Received data: " . $data . "\n";
}
socket_write()
The socket_write() function in PHP is used to send data through a socket. It allows you to transmit data to the remote endpoint with which you have established a connection.
// Connect to a remote server
socket_connect($socket, '127.0.0.1', 8080);
// Send data through the socket
$data = "Hello, server! I am Sanjay";
$bytesSent = socket_write($socket, $data, strlen($data));
if ($bytesSent === false) {
// Handle write failure
echo "Failed to write to socket: " . socket_strerror(socket_last_error()) . "\n";
} else {
// Data sent successfully
echo "Sent $bytesSent bytes to the server.\n";
}
socket_close()
The socket_close() function in PHP is used to close a socket connection. It releases the resources associated with the socket and terminates the connection.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Connect to a remote server
socket_connect($socket, '127.0.0.1', 8080);
// Perform operations with the socket
// Close the socket
socket_close($socket);
You can use these PHP socket functions to establish, bind, connect, listen, accept, read from, write to, and close network sockets. They give PHP users low-level access to socket programming capabilities.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.