PHP警告:socket_bind():主机查找失败[11001]:

发布于 2025-01-24 10:09:02 字数 6121 浏览 3 评论 0原文

我已经在YouTube上尝试了所有可能的解决方案,主机查找上的Google失败了,但仍然有一个错误的“ PHP警告:Socket_Bind():主机查找失败[11001]:“

这是我的PHP插座上的代码。

 <?php
    define('HOST_NAME',"localhost"); 
    // define('HOST_NAME',"192.168.43.49"); 
    define('PORT',"2306");
    $null = NULL;
    
    class _sHandler {
        function send($message) {
            global $clientSocketArray;
            $messageLength = strlen($message);
            foreach($clientSocketArray as $clientSocket)
            {
                @socket_write($clientSocket,$message,$messageLength);
            }
            return true;
        }
    
        function unseal($socketData) {
            $length = ord($socketData[1]) & 127;
            if($length == 126) {
                $masks = substr($socketData, 4, 4);
                $data = substr($socketData, 8);
            }
            elseif($length == 127) {
                $masks = substr($socketData, 10, 4);
                $data = substr($socketData, 14);
            }
            else {
                $masks = substr($socketData, 2, 4);
                $data = substr($socketData, 6);
            }
            $socketData = "";
            for ($i = 0; $i < strlen($data); ++$i) {
                $socketData .= $data[$i] ^ $masks[$i%4];
            }
            return $socketData;
        }
    
        function seal($socketData) {
            $b1 = 0x80 | (0x1 & 0x0f);
            $length = strlen($socketData);
            
            if($length <= 125)
                $header = pack('CC', $b1, $length);
            elseif($length > 125 && $length < 65536)
                $header = pack('CCn', $b1, 126, $length);
            elseif($length >= 65536)
                $header = pack('CCNN', $b1, 127, $length);
            return $header.$socketData;
        }
    
        function doHandshake($received_header,$client_socket_resource, $host_name, $port) {
            $headers = array();
            $lines = preg_split("/\r\n/", $received_header);
            foreach($lines as $line)
            {
                $line = chop($line);
                if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
                {
                    $headers[$matches[1]] = $matches[2];
                }
            }
    
            $secKey = $headers['Sec-WebSocket-Key'];
            $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
            $buffer  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: $host_name\r\n" .
            "WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n".
            "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
            socket_write($client_socket_resource,$buffer,strlen($buffer));
        }
        
        function newConnectionACK($client_ip_address) {
            $message = 'New client ' . $client_ip_address.' joined';
            $messageArray = array('connection_message'=>$message,'connection_message_type'=>'chat-connection-ack');
            $ACK = $this->seal(json_encode($messageArray));
            return $ACK;
        }
        
        function connectionDisconnectACK($client_ip_address) {
            $message = 'Client ' . $client_ip_address.' disconnected';
            $messageArray = array('message'=>$message,'message_type'=>'chat-connection-ack');
            $ACK = $this->seal(json_encode($messageArray));
            return $ACK;
        }
        
    }
    $_sHandler = new _sHandler();
    
    $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
    socket_bind($socketResource, 0, PORT);
    socket_listen($socketResource);
    
    $clientSocketArray = array($socketResource);
    while (true) {
        $newSocketArray = $clientSocketArray;
        socket_select($newSocketArray, $null, $null, 0, 10);
        
        if (in_array($socketResource, $newSocketArray)) {
            $newSocket = socket_accept($socketResource);
            $clientSocketArray[] = $newSocket;
            
            $header = socket_read($newSocket, 1024);
            $_sHandler->doHandshake($header, $newSocket, HOST_NAME, PORT);
            
            socket_getpeername($newSocket, $client_ip_address);
            $connectionACK = $_sHandler->newConnectionACK($client_ip_address);
            
            $_sHandler->send($connectionACK);
            
            $newSocketIndex = array_search($socketResource, $newSocketArray);
            unset($newSocketArray[$newSocketIndex]);
        }
        
        foreach ($newSocketArray as $newSocketArrayResource) {  
            while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1){
                $socketMessage = $_sHandler->unseal($socketData);
                $messageObj = json_decode($socketMessage);
                $_sHandler->send($_sHandler->seal(json_encode($messageObj)));
                break 2;
            }
            
            $socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
            if ($socketData === false) { 
                socket_getpeername($newSocketArrayResource, $client_ip_address);
                // $connectionACK = $_sHandler->connectionDisconnectACK($client_ip_address);
                $_sHandler->send($connectionACK);
                $newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
                unset($clientSocketArray[$newSocketIndex]);         
            }
        }
    }
    socket_close($socketResource);

从第94号线和95行中,我总是在这部分上有一个错误:

$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socketResource, 0, PORT);
socket_listen($socketResource);

根据上图,我该怎么办来修复套接字。

I already tried all the possible solution on YouTube, Google on the Host Lookup failed but still I got an error of "PHP Warning: socket_bind(): Host lookup failed [11001]:"

Error on php-sockets.php

Here's my code on the PHP socket.

 <?php
    define('HOST_NAME',"localhost"); 
    // define('HOST_NAME',"192.168.43.49"); 
    define('PORT',"2306");
    $null = NULL;
    
    class _sHandler {
        function send($message) {
            global $clientSocketArray;
            $messageLength = strlen($message);
            foreach($clientSocketArray as $clientSocket)
            {
                @socket_write($clientSocket,$message,$messageLength);
            }
            return true;
        }
    
        function unseal($socketData) {
            $length = ord($socketData[1]) & 127;
            if($length == 126) {
                $masks = substr($socketData, 4, 4);
                $data = substr($socketData, 8);
            }
            elseif($length == 127) {
                $masks = substr($socketData, 10, 4);
                $data = substr($socketData, 14);
            }
            else {
                $masks = substr($socketData, 2, 4);
                $data = substr($socketData, 6);
            }
            $socketData = "";
            for ($i = 0; $i < strlen($data); ++$i) {
                $socketData .= $data[$i] ^ $masks[$i%4];
            }
            return $socketData;
        }
    
        function seal($socketData) {
            $b1 = 0x80 | (0x1 & 0x0f);
            $length = strlen($socketData);
            
            if($length <= 125)
                $header = pack('CC', $b1, $length);
            elseif($length > 125 && $length < 65536)
                $header = pack('CCn', $b1, 126, $length);
            elseif($length >= 65536)
                $header = pack('CCNN', $b1, 127, $length);
            return $header.$socketData;
        }
    
        function doHandshake($received_header,$client_socket_resource, $host_name, $port) {
            $headers = array();
            $lines = preg_split("/\r\n/", $received_header);
            foreach($lines as $line)
            {
                $line = chop($line);
                if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
                {
                    $headers[$matches[1]] = $matches[2];
                }
            }
    
            $secKey = $headers['Sec-WebSocket-Key'];
            $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
            $buffer  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: $host_name\r\n" .
            "WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n".
            "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
            socket_write($client_socket_resource,$buffer,strlen($buffer));
        }
        
        function newConnectionACK($client_ip_address) {
            $message = 'New client ' . $client_ip_address.' joined';
            $messageArray = array('connection_message'=>$message,'connection_message_type'=>'chat-connection-ack');
            $ACK = $this->seal(json_encode($messageArray));
            return $ACK;
        }
        
        function connectionDisconnectACK($client_ip_address) {
            $message = 'Client ' . $client_ip_address.' disconnected';
            $messageArray = array('message'=>$message,'message_type'=>'chat-connection-ack');
            $ACK = $this->seal(json_encode($messageArray));
            return $ACK;
        }
        
    }
    $_sHandler = new _sHandler();
    
    $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
    socket_bind($socketResource, 0, PORT);
    socket_listen($socketResource);
    
    $clientSocketArray = array($socketResource);
    while (true) {
        $newSocketArray = $clientSocketArray;
        socket_select($newSocketArray, $null, $null, 0, 10);
        
        if (in_array($socketResource, $newSocketArray)) {
            $newSocket = socket_accept($socketResource);
            $clientSocketArray[] = $newSocket;
            
            $header = socket_read($newSocket, 1024);
            $_sHandler->doHandshake($header, $newSocket, HOST_NAME, PORT);
            
            socket_getpeername($newSocket, $client_ip_address);
            $connectionACK = $_sHandler->newConnectionACK($client_ip_address);
            
            $_sHandler->send($connectionACK);
            
            $newSocketIndex = array_search($socketResource, $newSocketArray);
            unset($newSocketArray[$newSocketIndex]);
        }
        
        foreach ($newSocketArray as $newSocketArrayResource) {  
            while(socket_recv($newSocketArrayResource, $socketData, 1024, 0) >= 1){
                $socketMessage = $_sHandler->unseal($socketData);
                $messageObj = json_decode($socketMessage);
                $_sHandler->send($_sHandler->seal(json_encode($messageObj)));
                break 2;
            }
            
            $socketData = @socket_read($newSocketArrayResource, 1024, PHP_NORMAL_READ);
            if ($socketData === false) { 
                socket_getpeername($newSocketArrayResource, $client_ip_address);
                // $connectionACK = $_sHandler->connectionDisconnectACK($client_ip_address);
                $_sHandler->send($connectionACK);
                $newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
                unset($clientSocketArray[$newSocketIndex]);         
            }
        }
    }
    socket_close($socketResource);

from line 94 and 95 I always got an error on this part:

$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socketResource, 0, PORT);
socket_listen($socketResource);

What should I do to fix the socketresource based on the image above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2025-01-31 10:09:02

首先,您需要遵循命令的正确协议。...

socket_bind($socketResource, 0, PORT);

第二个参数必须是一个地址。
php.com

这就是为什么您会收到消息(它告诉您问题 - 有点...)关于“无效的参数”。

我尝试了您的代码,但还有其他一些问题,但是您的问题是关于此行上的错误,因此看来您仍在努力。

将您要连接的地址放在那里而不是“ 0” - 这应该使您再次前进。

First off, you need to follow the proper protocol for the command....

socket_bind($socketResource, 0, PORT);

the second argument MUST be an address....
PHP.com

that is why you are getting the message (which tells you the issue - somewhat...) about an 'invalid argument'.

I tried your code and it has some other issues, but your question was about the error on this line, so it seems you are still working on it.

Put the address you want to connect with in there instead of '0' - that should get you going again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文