Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP 應用之一 socket funion : 偽 WEB Server

1,490 views

Published on

PHP 應用之一 socket funion : 偽 WEB Server

Published in: Internet
  • Be the first to comment

  • Be the first to like this

PHP 應用之一 socket funion : 偽 WEB Server

  1. 1. php 應用之一 socket funion 黃志賢 hoyo www.hoyo.idv.tw
  2. 2. 應用系列說明 Socket Functions Filesystem Functions Output Control Functions Process Control Functions Shared Memory and IPC Functions 截至目前有 182 個函式庫
  3. 3. Command Line Interface
  4. 4. 第一階段:Socket 了解 Server / Client 在通訊時的差別 了解 TCP / UDP 的特性 可以建立 Client 連線到 Server 要求資料 可以建立 Server Listen 環境提供服務 應用一:偽 web server
  5. 5. 第二階段:Server Push 了解 Server Push 的特性 透過記憶體交換達到即時效果 : : 應用二:php 聊天室
  6. 6. 本片開始
  7. 7. Socket Function socket_ accept socket_ bind socket_ close socket_ connect socket_ create socket_ listen socket_ read socket_ recv socket_ recvfrom socket_ send socket_ sendto socket_ shutdown socket_ write
  8. 8. TCP Client 建立 $socket = @socket_create ( AF_INET,SOCK_STREAM, SOL_TCP); @socket_connect( $socket, $ipaddress, $port ); 怎麼問 socket_write($socket,$command,strlen( $command )); 怎麼收 $read = @socket_read ($socket, 64);
  9. 9. TCP Server 建立 $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ); socket_bind ( $socket, $address, $port ); socket_listen ( $socket ); 怎麼等 $msgsock = @socket_accept ( $socket ); while( $buff = @socket_read ( $msgsock, 1024 ) ) { } 怎麼回 socket_send( $msgsock, $desc, strlen($desc), 0 );
  10. 10. UDP Client 建立 $sock_udp = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); 怎麼丟 socket_sendto($sock_udp, $temp, strlen($temp), 0, '239.255.255.250', 1900);
  11. 11. UDP Server 建立 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind ( $socket, $address, $port ); 死命的收 while (1) { @socket_recvfrom( $socket, $buffer, 2048, 0, $clientip, $clientport ); }
  12. 12. 請看操演 找一位助理
  13. 13. TCP Client
  14. 14. TCP Server
  15. 15. UDP Client
  16. 16. UDP Server
  17. 17. 實作 HyperText Transfer Protocol http://www.w3.org/Protocols/
  18. 18. demo php web.php &
  19. 19. Internet Explorer 6.0 GET / HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shock wave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application /msword, */* Accept-Language: zh-tw Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Med ia Center PC 3.0; .NET CLR 1.0.3705) Host: 127.0.0.1:800 Connection: Keep-Alive
  20. 20. Firefox 2.0 GET / HTTP/1.1 Host: 127.0.0.1:800 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.1) Gecko/20 061204 Firefox/2.0.0.1 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plai n;q=0.8,image/png,*/*;q=0.5 Accept-Language: zh-tw,en-us;q=0.7,en;q=0.3 Accept-Encoding: gzip,deflate Accept-Charset: Big5,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive
  21. 21. 使用 netstat 檢視連線狀態
  22. 22. HTTP 通訊流程 瀏覽器 WEB Server 判斷想要取得的檔案 將設定的首頁傳回
  23. 23. HTTP 通訊流程 瀏覽器 WEB Server Listen Port ex:800 將設定的首頁傳回 Open Port ex:1217 在未設定下,Client 和 server 通訊時, 會使用那一個 port 是無法預期的
  24. 24. 參考資源 TCP 與 UDP http://www.study-area.org/network/network_ip_tcp.htm TCP/IP 工作模型 http://www.study-area.org/network/network_ip_model.htm
  25. 25. 參考工具 netstat :: 得知連線狀態 echo :: 顯示程式結果 What Is Transferring :: TCP/UDP Ethereal :: ALL (UPnP)

×