问题描述
这让我发疯了.我必须在php 5.3中创建一个非常简单的非阻塞套接字脚本,在该脚本中,客户端都使用非阻塞套接字连接到服务器.
我尝试了 phpsocketdaemon 和 php手册,但是在两种情况下,当我尝试连接到服务器时,都会出现以下错误:
socket_connect() [function.socket-connect]: unable to connect [10035]:
A non-blocking socket operation could not be completed immediately
发生错误的我的客户端脚本:
$service_port = 2002;
$address = '127.0.0.1';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
$result = socket_connect($socket, $address, $service_port);
...
我正在Win 7上使用Zend Server 5.6.0 SP1的免费版本.
有人知道如何解决此问题,或者知道无阻塞套接字客户端/服务器脚本的简单易懂的示例吗?
当您设置套接字为非阻塞时,您不能指望socket_connect()
的结果在连接时返回TRUE,否则返回FALSE.
在任何语言中都是如此.您必须将套接字设置为阻塞",或者必须在文件描述符上进行轮询/选择,然后检查是否正确连接.在PHP中,您可能会在一段时间后调用socket_connect()
函数,以检查它是否返回true,false或等待超时到期.
尝试使用此代码[ EDITED 修复超时例程中的小错误]:
<?php
$service_port = 2002;
$address = '127.0.0.1';
$timeout = 3;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
$error = NULL;
$attempts = 0;
$timeout *= 1000; // adjust because we sleeping in 1 millisecond increments
$connected = FALSE;
while (!($connected = @socket_connect($socket, $address, $service_port)) && ($attempts++ < $timeout)) {
$error = socket_last_error();
if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
echo "Error Connecting Socket: ".socket_strerror($error) . "\n";
socket_close($socket);
return NULL;
}
usleep(1000);
}
if (!$connected) {
echo "Error Connecting Socket: Connect Timed Out After " . $timeout/1000 . " seconds. ".socket_strerror(socket_last_error()) . "\n";
socket_close($socket);
return NULL;
}
?>
This makes me nuts. I have to create a very simple non-blocking socket script in php 5.3 where a client connects to a server, both using non-blocking sockets.
I've tried phpsocketdaemon and the example from the php manual, but in both cases when I try to connect to the server I get the following error:
socket_connect() [function.socket-connect]: unable to connect [10035]:
A non-blocking socket operation could not be completed immediately
My client script where the error happens:
$service_port = 2002;
$address = '127.0.0.1';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
$result = socket_connect($socket, $address, $service_port);
...
I'm using the free version of Zend Server 5.6.0 SP1 on Win 7.
Does anyone know how to fix this problem or know a simple and understandable example of a non-blocking socket client/server script?
When you set your socket non blocking you cannot expect that the result of socket_connect()
returns TRUE if it's connected or FALSE if not.
This is true in any language. You have to set the socket "blocking" or you have to poll/select on your file descriptor before checking if you are correctly connected or not.In PHP you may recall the socket_connect()
function after a small period of time to check if it returns true, false or wait for timeout to expire.
Try this code [EDITED to fix a small error on timeout routine]:
<?php
$service_port = 2002;
$address = '127.0.0.1';
$timeout = 3;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($socket);
$error = NULL;
$attempts = 0;
$timeout *= 1000; // adjust because we sleeping in 1 millisecond increments
$connected = FALSE;
while (!($connected = @socket_connect($socket, $address, $service_port)) && ($attempts++ < $timeout)) {
$error = socket_last_error();
if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
echo "Error Connecting Socket: ".socket_strerror($error) . "\n";
socket_close($socket);
return NULL;
}
usleep(1000);
}
if (!$connected) {
echo "Error Connecting Socket: Connect Timed Out After " . $timeout/1000 . " seconds. ".socket_strerror(socket_last_error()) . "\n";
socket_close($socket);
return NULL;
}
?>
这篇关于无法连接到非阻塞插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!