真是惭愧,花费了一天的时间,才调试出来,暂时还未进行代码优化。
[server.php]
<?php
$addr = "192.168.2.225";
$readdr = "192.168.2.60";
$port = "20000";
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket < 0) {
echo "Socket create:".$socket_strerror($socket)."/n";
exit;
}
if ( ($ret = socket_bind($socket, $addr, $port)) < 0 ) {
echo "socket bind:".socket_strerror()."/n";
exit;
}
if ( ($ret = socket_listen($socket, 5)) < 0 ) {
echo "socket listen:".socket_strerror()."/n";
exit;
}
socket_set_nonblock($socket);
echo "Wainting fro a connection:/n";
$clients = array($socket);
while(true) {
$read = $clients;
if(socket_select($read, $writes=NULL, $execs=NULL, 10) < 1)
continue;
if(in_array($socket,$read)) {
$clients[] = $newsock = socket_accept($socket);
$msg = "Server: $addr/r/n".
"Notice:Welcome user server./r/n";
socket_write($newsock,$msg);
socket_set_nonblock($newsock);
socket_getpeername($newsock,$readdr);
$key = array_search($socket,$read);
unset($read[$key]);
}
foreach($read as $read_sock) {
$data = @socket_read($read_sock,1024,PHP_BINARY_READ);
if($data == false) {
$key = array_search($read_sock,$clients);
unset($clients[$key]);
socket_close($read_sock);
echo "Client disconnected./n";
continue;
}
$sourceFile = trim($data);
$dest = "/"p:/eee.pdf/"";
$cmd = "html2pdf /"$sourceFile/" $dest/r/n";
echo $cmd;
exec($cmd,$ret_arr);
socket_write($read_sock,"ok./n");
exit;
}
}
socket_close($socket);
?>
[client.php]
<?php
$addr = "192.168.2.225";
$port = "20000";
$fp = fsockopen($addr,$port,$errno,$errstr,30);
if(!$fp) {
echo "$errstr ($errno) /n";
exit;
}
else {
$str = "p://abc.doc/r/n";
fwrite($fp,$str);
while(!feof($fp)) {
$filename = fgets($fp,1024);
echo $filename;
}
fclose($fp);
}
?>