<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # socket_create_pair * * * * * 說明: > 創建一對無名的套接字,并將它們存儲在一個數組中,這對套接字可以進行雙工通信,每一個描述符既可以讀也可以寫。這個在同一個進程中也可以進行通信,向s[0]中寫入,就可以從s[1]中讀取(只能從s[1]中讀取),也可以在s[1]中寫入,然后從s[0]中讀取;但是,若沒有在0端寫入,而從1端讀取,則1端的讀取操作會阻塞,即使在1端寫入,也不能從1讀取,仍然阻塞;反之亦然...... 語法: ~~~ bool socket_create_pair ( int $domain , int $type , int $protocol , array &$fd ) ~~~ 參數: | 參數 | 描述 | | --- | --- | | domain | 為創建的套接字指定協議| | type | 套接字使用的類型| | protocol | 套接字使用的類型| | fd | 包含兩個套接字資源的引用數組| domain,type,protocol參數具體選項可參考 http://www.hmoore.net/mangyusisha/php/550787 返回值: | 成功 | 失敗 | | --- | --- | | true | false | 返回失敗,錯誤代碼會傳入 socket_last_error() ,如果將此參數傳入 socket_strerror() 則可以得到錯誤的文字說明。 技術細節: PHP版本:PHP 4 >= 4.1.0, PHP 5, PHP 7 實例一:同進程的通訊 socket_create_pair_example.php ~~~ <?php $sockets = array(); // window 需要使用 AF_INET 協議 $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX); //創建一對無名的套接字,并將它們存儲在 $sockets 數組中 if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) { echo "socket_create_pair failed. Reason: " . socket_strerror(socket_last_error()); } // 寫入數據 $write_string = "ABCdef123\n"; $write_string_length = strlen($write_string); if (socket_write($sockets[0], $write_string, $write_string_length) === false) { echo "socket_write() failed. Reason: " . socket_strerror(socket_last_error($sockets[0])); } //讀取 上面寫入的數據 if (($read_string = socket_read($sockets[1], $write_string_length, PHP_BINARY_READ)) === false) { echo "socket_read() failed. Reason: " . socket_strerror(socket_last_error($sockets[1])); } var_dump($read_string); /* 關閉套接字 */ socket_close($sockets[0]); socket_close($sockets[1]); ~~~ 打開windows的cmd窗口,然后,運行php socket_create_pair_example.php ![](https://box.kancloud.cn/a0e132c56dd69ffb81f0a6b7df60a8b0_325x54.png) socket_create_pair創建的套接字可以進行雙工通信,可以把$sockets[0]和$sockets[1]位置對調 運行程序也是一樣的結果 實例二:不同進程直接的通訊(以下實例不能在window運行) socket_create_pair_ipc_example.php ~~~ $parent_message = '我是父級進程'; $child_message = '我是子進程'; $sockets = array(); // window 需要使用 AF_INET 協議 $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX); //創建一對無名的套接字,并將它們存儲在 $sockets 數組中 if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) { echo "socket_create_pair failed. Reason: " . socket_strerror(socket_last_error()); } //創建一個新的進程(子進程) 子進程和父進程 從這里開始往下執行 $pid = pcntl_fork(); if ($pid == -1) { echo '錯誤,不能創建進程'; } elseif ($pid) { // 父進程 socket_close($sockets[0]); if (socket_write($sockets[1], $parent_message, strlen($parent_message)) === false) { echo "socket_write() failed. Reason: " . socket_strerror(socket_last_error($sockets[1])); } if (socket_read($sockets[1], strlen($child_message), PHP_BINARY_READ) == $child_message) { echo "父進程收到子進程信息: $child_message\n"; } socket_close($sockets[1]); } else { // 子進程 socket_close($sockets[1]); if (socket_write($sockets[0], $child_message, strlen($child_message)) === false) { echo "socket_write() failed. Reason: " . socket_strerror(socket_last_error($sockets[0])); } if (socket_read($sockets[0], strlen($parent_message), PHP_BINARY_READ) == $parent_message) { echo "子進程收到父進程信息: $parent_message\n"; } socket_close($sockets[0]); } ~~~ 在linux 中 運行php socket_create_pair_ipc_example.php ![](https://box.kancloud.cn/fca19b2e167bfc57829a34bc45d35c55_630x62.png) 注: 1、需要開啟sockets和 pcntl 擴展 資料來源: http://php.net/manual/zh/function.socket-create-pair.php http://blog.51cto.com/liulixiaoyao/533469
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看