PHP網路函數fsockopen如何?Socket連結_PHP教程

來源:互聯網
上載者:User
文法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);

傳回值: 整數

函數種類: 網路系統內容說明: 目前PHP網路函數fsockopen提供二個 Socket 資料流介面,分別為 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。當在 Internet 情形下使用時,參數 hostname 及 port 分別代表網址及埠號。在 UNIX 情形可做 IPC,hostname 參數表示到 socket 的路徑,port 配置為 0。可省略的 timeout 選項表示多久沒有連上就中斷。在使用本函數之後會返迴文件指標,供檔案函數使用,包括 fgets()、fgetss()、fputs()、fclose() 與 feof()。參數 errno 及 errstr 也是可省略的,主要當做錯誤處理使用。使用本函數,會使用擱置模式 (blocking mode) 處理,可用set_socket_blocking() 轉換成無擱置模式。

PHP網路函數fsockopen的使用範例,本例用來類比成 HTTP 串連。

 
  1. php
  2. $fp = fsockopen("php.wilson.gs", 80, &$errno, &$errstr, 10);
  3. if(!$fp) {
  4. echo "$errstr ($errno)<br>n";
  5. } else {
  6. fputs($fp,"GET / HTTP/1.0nHost: php.wilson.gsnn");
  7. while(!feof($fp)) {
  8. echo fgets($fp,128);
  9. }
  10. fclose($fp);
  11. }
  12. ?>

通過上面這段PHP網路函數fsockopen的使用樣本,讀者朋友們是不是已經能夠基本掌握了這個函數的用法了呢?


http://www.bkjia.com/PHPjc/446295.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446295.htmlTechArticle文法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]); 傳回值: 整數 函數種類: 網路系統內容說明: 目前PHP網路函數...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.