<pre name="code" class="html"><?php
/*
php socket 需要开启socket扩展
服务器返回值应该以\n或\r结束.
由服务器端生成的socket文件,应该将权限改成apache用户组的.如在linux shell下:chown apache:apache /tmp/mcpcc-web.sock
apache为apache服务器的用户名,可从配置文件中查看,asianux默认用户名为apache
*/
function client($sendData){
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if(!$socket){
return ;
}else{
}
$conn = socket_connect($socket, "/tmp/mcpdas.sock");
if(!$conn){
return ;
}else{
}
if(!socket_write($socket, $sendData, strlen($sendData))){
return ;
}else{
}
while(true){
$read = array($socket);
$write = NULL;
$except = NULL;
$socketChanged = socket_select($read, $write, $except, 10);
if ($socketChanged == false){
break;
}else if ($socketChanged == 0){
return "Time out";
break;
}else if ($socketChanged > 0){
// PHP_NORMAL_READ - reading stops at \n or \r.
if ($readReturned = socket_read($read[0], 2048, PHP_NORMAL_READ )){
return $readReturned;
}else{
break;
}
}
}
socket_close($socket);
}
?>