dongpu7881 2017-05-04 08:12
浏览 183

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug