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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面