【问题标题】:How to send data to a specific socket如何将数据发送到特定的套接字
【发布时间】:2014-09-27 06:18:08
【问题描述】:

我是使用 PHP Websocket 的新手。 这是我的问题:

我已经成功运行 PHP websocket 并创建了一个简单的聊天应用程序(当然是 web 应用程序)。在接收客户数据时。所有客户端都将收到数据。我如何将数据发送到指定的客户端或多个客户端(不是所有客户端)。

我在here学习它

【问题讨论】:

    标签: php json phpwebsocket


    【解决方案1】:
    <?php
      // Create a new socket
      $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    
      // An example list of IP addresses owned by the computer
      $sourceips['kevin']    = '127.0.0.1';
      $sourceips['madcoder'] = '127.0.0.2';
    
      // Bind the source address
      socket_bind($sock, $sourceips['madcoder']);
    
      // Connect to destination address
      socket_connect($sock, '127.0.0.1', 80);
    
      // Write
      $request = 'GET / HTTP/1.1' . "\r\n" .
           'Host: example.com' . "\r\n\r\n";
      socket_write($sock, $request);
    
      // Close
      socket_close($sock);
    
    ?>
    

    来源:http://php.net/manual/en/function.socket-bind.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多