socket_connect正在工作,但socket_bind没有显示warring

时间:2017-10-09 14:46:56

标签: php sockets

使用我的代码,我可以正常连接但套接字绑定不起作用请帮助我。

显示此错误:

  

警告:socket_bind():无法绑定地址[99]:无法在第35行的/opt/lampp/htdocs/testingServer/socket.php中分配请求的地址

     

socket_bind()失败:原因:无法分配请求的地址

     

警告:socket_listen():无法侦听socket [22]:第39行/opt/lampp/htdocs/testingServer/socket.php中的参数无效

     

socket_listen()失败:原因:参数无效

以下是代码:

$address = '192.168.0.250';
$port = 8050;
/* Get the port for the WWW service. */
// echo $port = getservbyname('www', 'tcp');  

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
  echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "<pre/>";
    print_r($sock);
    echo "Socket created"; echo "<br/>";
} 
 // check ip and port showing online or offline
 if (!$socket = @fsockopen($address, $port, $errno, $errstr, 3)) {
        echo "Offline!";
    } else {
        echo "Online!";
        fclose($socket);
    } 

 $result = socket_connect($sock, $address, $port) ;
 var_dump($result);
 if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . 
          socket_strerror(socket_last_error($sock)) . "\n";
} 

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

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

 $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($sock, $msg, strlen($msg)); 
socket_close($sock);

0 个答案:

没有答案