socket_select()不是有效的套接字

时间:2016-09-05 06:13:19

标签: php sockets

我收到了这个错误:

  

警告:socket_select():提供的参数不是第12行的/volume1/web/is/xxxx/listen-new.php中的有效Socket资源   PHP警告:socket_select():提供的参数不是第12行/volume1/web/is/xxxx/listen-new.php中的有效Socket资源   这是我的代码段

我的代码是:

    $port = $this->port;
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
    socket_bind($sock, 0, $port);
    socket_listen($sock);
    $this->clients[] = $sock;
    $data = array();        
    while (true) {
        $read = $this->clients;
        $write = array(); //NULL
        $except = array();//NULL
        $sckt = socket_select($read, $write, $except, 0);
        if($sckt === false){
             echo "socket_select() failed, reason: " .
                    socket_strerror(socket_last_error()) . "\n";
        }
        elseif($sckt > 0) {
            if (in_array($sock, $read)) {
                $this->clients[] = $newsock = socket_accept($sock);
                //var_dump($this->gps);
                socket_write($newsock, "Connected\n");
                //var_dump($this->gps);
                /**try{
                    socket_getpeername($newsock, $ip); //error
                    echo "New client connected: {$ip}\n";              
                }
                catch(Exception $e){
                    echo "error : $e->getMessage()\n";
                }**/
                $key = array_search($sock, $read);
                unset($read[$key]);
            }           
            foreach ($read as $read_sock) {
                $data = @socket_read($read_sock, 2048);             
                if ($data === false) {
                    $gpsdisc = array_search($read_sock, array_column($this->gps, 'pid'));
                    $key = array_search($read_sock, $this->clients);
                    unset($this->clients[$key]);
                    unset($this->gps[$gpsdisc]);
                    echo "client disconnected.\n";


                    continue;
                }
                else{
                    $data = trim($data);
                    if (!empty($data)) {
                        var_dump($data);
                        $buf = bin2hex($data);
                        $start = substr($buf, 0,4);     
                        if($start=="7878"){
                            $protocol = substr($buf, 6,2);                          
                            if($protocol=="01"){
                                $imei = substr($buf, 8,16);
                                $cariGPS = $this->cariGPS($imei);
                                if($cariGPS!=NULL){
                                    $this->setGPS($imei,$read_sock,$cariGPS);
                                    $reply = $this->authLogin($data);
                                    $rep = hex2bin("$reply");
                                    socket_write($read_sock, $rep, strlen($rep));
                                }
                                else
                                    echo "$imei salah";
                            }
                            elseif($protocol=="15"){
                                //echo "$buf\n";
                                $this->reply($data);
                            }
                            elseif($protocol=="12"){
                                $hex12 = bin2hex($data);
                                //echo "$hex12\n";
                            }
                            else{
                                $hex12 = bin2hex($data);                            
                                echo "$hex12\n";
                            }

                            echo "$protocol\n";
                        }
                        else{
                            if($data=="where"){
                                //echo $data."\n";
                                $this->where($this->gps);                                   
                            }
                            elseif($data=="quit"){
                                $gpsdisc = array_search($read_sock, array_column($this->gps, 'pid'));
                                unset($this->gps[$gpsdisc]);
                                socket_close($read_sock);
                                $key = array_search($read_sock, $this->clients);
                                unset($this->clients[$key]);
                            }
                            else{
                                echo "command not found\n";
                            }
                        }

                    }
                }              
            }
        }
    }
    socket_close($sock);

PS 我还必须更改$write = null$except = null

对此有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

看起来您只提供了服务器代码,但省略了客户端代码。你能发布客户代码吗?我猜问题就在那里。使用我的测试客户端(我使用socket_create),我无法重现您报告的问题。 我的也是php7(如果你很好奇)。 这可能会为您提供一些提示:Socket_read() says "not a valid resource"