When using PHP's socket_accept() function for Socket programming, a common warning message is:
Warning: socket_accept() expects parameter 1 to be resource, bool given
The error means: The socket_accept() function expects the received parameter to be a resource, but in fact it is passed a Boolean value (usually false ). So why is a value of Boolean type passed in instead of the expected Socket resource? The root cause is that a step in socket_create() or socket_bind() or socket_listen() fails.
Let’s take a look at a typical Socket server code structure:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0', 8080);
socket_listen($socket);
while (true) {
$client = socket_accept($socket);
// ...Handle client connections
}
If one of the rows (such as socket_create() ) returns false , then $socket is not a valid Socket resource. An error occurs when calling socket_accept($socket) : a boolean value (false) is passed in, not a valid resource.
We can use socket_last_error() and socket_strerror() functions to output detailed error information:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
if (!socket_bind($socket, '0.0.0.0', 8080)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
if (!socket_listen($socket)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
while (true) {
$client = socket_accept($socket);
if ($client === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
$msg = "Welcome M66 Socket Serve!\n";
socket_write($client, $msg, strlen($msg));
socket_close($client);
}
In this way, it is easier to locate which function call fails, thereby fixing the underlying problem.
Insufficient permissions <br> If you try to bind to a port below 1024, such as 80 or 21, it will fail due to insufficient permissions. The solution is to use ports above 1024, or run the script with administrator privileges.
The port has been occupied <br> If the port has been occupied by other processes, socket_bind() will fail. You can use commands such as netstat -tlnp (Linux) or netstat -ano (Windows) to view port occupancy.
Illegal binding address <br> If the specified IP address is illegal or the machine is not configured, socket_bind() will fail. Ensure that the bound address is legal and valid, such as 0.0.0.0 (binding all IPs) or the actual intranet IP of the machine.
PHP Socket extension not loading correctly <br> Make sure the sockets extension is enabled in php.ini . Can be checked via phpinfo() or extension_loaded('sockets') .
<?php
$host = '0.0.0.0';
$port = 9000;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
die("socket_create() fail: " . socket_strerror(socket_last_error()) . "\n");
}
if (!socket_bind($socket, $host, $port)) {
die("socket_bind() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
if (!socket_listen($socket)) {
die("socket_listen() fail: " . socket_strerror(socket_last_error($socket)) . "\n");
}
echo "Listening: $host:$port\n";
while (true) {
$client = socket_accept($socket);
if ($client === false) {
echo "socket_accept() fail: " . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
$welcome = "Welcome to visit m66.net of Socket Serve!\n";
socket_write($client, $welcome, strlen($welcome));
socket_close($client);
}
socket_close($socket);
When you encounter an error of "expects parameter 1 to be resource" when using socket_accept() , don't directly think that the problem lies in socket_accept() , but you should back-track whether the previous socket_create() , socket_bind() and socket_listen() were executed successfully. By adding error judgment and log output, you can quickly locate the problem and avoid wasting time on the wrong links.
I hope this article can help you understand this common problem more clearly and master the positioning and solutions.