Php Socket编程主机错误

我是套接字的新手 . 我从谷歌的代码中引用了它,但似乎没有用 .
我发布服务器和客户端PHP文件 . 请确定问题 .

Server.php

<?php
$host = "xxx.xxx.xxx.xxx/myfolder/server.php"; //host
$port = 9000; //port

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) ;
$result = socket_bind($socket, $host, $port);
$result = socket_listen($socket,5);

$spawn = socket_accept($socket);

$input = socket_read($spawn , 1024);

$output = strrev($input)."n";

socket_write($spawn, $output , strlen($output));

socket_close($spawn);
socket_close($socket);
?>

这是Client.php

<?
 $host    = "xxx.xxx.xxx.xxx/myfolder/server.php";
 $port    = 9000;

$socket = socket_create(AF_INET, SOCK_STREAM, 0) ;

$result = socket_connect($socket, $host, $port) ;

socket_write($socket, $message, strlen($message)) ;

$result = socket_read ($socket, 1024) ;
echo "Reply From Server  :".$result;

socket_close($socket);
?>

在我的主机上的公共目录上有上述两个文件之后 .

我先运行命令: php -q /var/www/html/myfolder/server.php
但我在我的cmd shell上得到这个:

$ php -q /var/www/html/myfolder/server.php PHP警告:socket_bind():主机查找失败[-10001]:第13行/var/www/html/myfolder/server.php中的未知主机无法在服务器上绑定套接字

(是的,端口9000是开放的)

回答(1)

3 years ago

创建套接字服务器时,不要将主机指定为脚本的完整URL;这就是绑定失败的原因 .

看一下这个样本:http://www.php.net/manual/en/sockets.examples.php

#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.1.53';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>