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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?