我已经在 Linux 2.6.34 上安装了 php 5.4.13。
我已经使用套接字制作了简单的客户端/服务器页面,但它不起作用。
它给出了权限被拒绝的错误
下面是我的php代码
if (false == ($socket = socket_create(AF_INET, SOCK_STREAM, 0))) // create socket
{
$stringData= date("D M d, Y g:i A"). " socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
}
else
{
$timeout = array('sec'=>5,'usec'=>500000);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
if(false==($result = socket_connect($socket, $host, $port)))
{
$stringData= date("D M d, Y g:i A"). " socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
else
{
$stringData= date("D M d, Y g:i A"). " Socket connected succefully <br>";
echo $stringData;
if(false==(socket_write($socket, $command, strlen($command))))
{
$stringData= date("D M d, Y g:i A"). " socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
else
{
if(false===($cmd = socket_read ($socket, 65536)))
{
//10060 for windows and 11 for linux
if(10060!=socket_last_error() && 11!=socket_last_error())
{
$stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
echo $stringData;
socket_close($socket);
}
switch(socket_select($r = array($socket), $w = array($socket), $f = array($socket), 5))
{
case 2:
$refused=1;
break;
}
if($refused==1)
{
$stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: Connection Refused <br>";
$ourFileHandle = fopen(SOCKET_LOG, 'a');
echo $stringData;
socket_close($socket);
}
}
else
{
echo "<pre>".html_entity_decode(print_r($cmd,true))."</pre>";
}
}
}
}
上面的代码在命令提示符下工作正常,但是当从任何浏览器打开页面时,它会给出错误权限被拒绝。
从终端运行 php 的命令:/usr/local/rootfs/php5/bin/php /www/socket_client.php