stream_socket_server不起作用,但socket_create工作

I'm trying to use websocket in a php5+ & win7x64 environment. At first, I use stream_socket_server to create a server socket by using php-websocket example, (saved as test_socket1.php)

....
$url = 'tcp://127.0.0.1:8000';
$this->context = stream_context_create();
if(!$this->master = stream_socket_server($url, $errno, $err, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $this->context))
{
   die('Error creating socket: ' . $err);
}   
$this->allsockets[] = $this->master;
$this->log('Server created');   
....
....
public function run()
{
    while(true)
    {
        $changed_sockets = $this->allsockets;
        @stream_select($changed_sockets, $write = null, $except = null, 0, 5000);   
        foreach($changed_sockets as $socket)
        {
            if($socket == $this->master)
            {
                if(($ressource = stream_socket_accept($this->master)) === false)
                {
                    $this->log('Socket error: ' . socket_strerror(socket_last_error($ressource)));
                    continue;
                }
                else
                {
....

And run command: php test_socket1.php, got "Server created" output. After that, I run client.html in chrome/IE, it will get into stream_select without any return till timeout. So I use another example, PHP-Websockets, which is using socket_create, (saved as test_socket2.php)

....    
$this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)  or die("Failed: socket_create()");
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option()");
socket_bind($this->master, '127.0.0.1', '8000')                      or die("Failed: socket_bind()");
socket_listen($this->master,20)                               or die("Failed: socket_listen()");
$this->sockets['m'] = $this->master;
$this->stdout("Server started
Master socket: ".$this->master);
....

And run command: php test_socket2.php, got "Server started..." output. then I run same client.html, works fine as respected. Why this one work? The client.html as below,

var host = "ws://127.0.0.1:8000"; // SET THIS TO YOUR SERVER
try {
    socket = new WebSocket(host);
    log('WebSocket - status '+socket.readyState);
    socket.onopen    = function(msg) { 
                           log("Welcome - status "+this.readyState); 
                       };
    socket.onmessage = function(msg) { 
                           log("Received: "+msg.data); 
                       };
    socket.onclose   = function(msg) { 
                           log("Disconnected - status "+this.readyState); 
                       };
}
catch(ex){ 
    log(ex); 
}

I also add some check points in test_socket1 trying to find out the problem but bad luck and has couple unused results,

  1. @stream_select --> won't return anything, but client.html has "WebSocket connection to 'ws://127.0.0.1:8000' failed: WebSocket opening handshake timed out" response.
  2. Change "@stream_select" to "@socket_select" (I know it's stupid), then it got none-zero returns but got error from "stream_socket_accept", "Warning: stream_socket_accept(): accept failed: connection timeout..." and the client.html got "WebSocket connection to 'ws://127.0.0.1:8000' failed: HTTP Authentication failed; no valid credentials available", looks like server is waiting for client's response and client is waiting for server's handshaking.

So my question is why those two has different results? and does stream_socket need more pre-config stuff? Thanks.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问