25

Click here to load reader

PHP 應用之一 socket funion : 偽 WEB Server

  • Upload
    -

  • View
    372

  • Download
    4

Embed Size (px)

Citation preview

Page 1: PHP 應用之一 socket funion : 偽 WEB Server

php 應用之一 socket funion

黃志賢 hoyo

www.hoyo.idv.tw

Page 2: PHP 應用之一 socket funion : 偽 WEB Server

應用系列說明

Socket Functions

Filesystem Functions

Output Control Functions

Process Control Functions

Shared Memory and IPC Functions

截至目前有 182 個函式庫

Page 3: PHP 應用之一 socket funion : 偽 WEB Server

Command Line

Interface

Page 4: PHP 應用之一 socket funion : 偽 WEB Server

第一階段:Socket

了解 Server / Client 在通訊時的差別

了解 TCP / UDP 的特性

可以建立 Client 連線到 Server 要求資料

可以建立 Server Listen 環境提供服務

應用一:偽 web server

Page 5: PHP 應用之一 socket funion : 偽 WEB Server

第二階段:Server Push

了解 Server Push 的特性

透過記憶體交換達到即時效果

:

:

應用二:php 聊天室

Page 6: PHP 應用之一 socket funion : 偽 WEB Server

本片開始

Page 7: PHP 應用之一 socket funion : 偽 WEB Server

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

Page 8: PHP 應用之一 socket funion : 偽 WEB Server

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);

Page 9: PHP 應用之一 socket funion : 偽 WEB Server

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 );

Page 10: PHP 應用之一 socket funion : 偽 WEB Server

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);

Page 11: PHP 應用之一 socket funion : 偽 WEB Server

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 );

}

Page 12: PHP 應用之一 socket funion : 偽 WEB Server

請看操演

找一位助理

Page 13: PHP 應用之一 socket funion : 偽 WEB Server

TCP Client

Page 14: PHP 應用之一 socket funion : 偽 WEB Server

TCP Server

Page 15: PHP 應用之一 socket funion : 偽 WEB Server

UDP Client

Page 16: PHP 應用之一 socket funion : 偽 WEB Server

UDP Server

Page 17: PHP 應用之一 socket funion : 偽 WEB Server

實作 HyperText Transfer Protocol

http://www.w3.org/Protocols/

Page 18: PHP 應用之一 socket funion : 偽 WEB Server

demo

php web.php &

Page 19: PHP 應用之一 socket funion : 偽 WEB Server

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

Page 20: PHP 應用之一 socket funion : 偽 WEB Server

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

Page 21: PHP 應用之一 socket funion : 偽 WEB Server

使用 netstat 檢視連線狀態

Page 22: PHP 應用之一 socket funion : 偽 WEB Server

HTTP 通訊流程

瀏覽器 WEB Server

判斷想要取得的檔案

將設定的首頁傳回

Page 23: PHP 應用之一 socket funion : 偽 WEB Server

HTTP 通訊流程

瀏覽器 WEB Server

Listen Port ex:800

將設定的首頁傳回

Open Port ex:1217

在未設定下,Client 和 server 通訊時,

會使用那一個 port 是無法預期的

Page 24: PHP 應用之一 socket funion : 偽 WEB Server

參考資源

TCP 與 UDP http://www.study-area.org/network/network_ip_tcp.htm

TCP/IP 工作模型 http://www.study-area.org/network/network_ip_model.htm

Page 25: PHP 應用之一 socket funion : 偽 WEB Server

參考工具

netstat :: 得知連線狀態

echo :: 顯示程式結果

What Is Transferring :: TCP/UDP

Ethereal :: ALL (UPnP)