本篇文章給大家分享的內容是關於PHP實現系統編程之網絡Socket及IO多路復用,有著一定的參考價值,有需要的朋友可以參考一下
一直以來,PHP很少用於socket編程,畢竟是一門腳本語言,效率會成為很大的瓶頸,但是不能說PHP就無法用於socket編程,也不能說PHP的socket編程性能就有多麼的低,例如知名的一款PHP socket框架workerman 就是用純PHP開發,號稱擁有優秀的性能,所以在某些環境下,PHP socket程式設計或許也可一展身手。
PHP提供了一系列類似C語言socket函式庫中的方法供我們呼叫:
##
socket_accept — Accepts a connection on a socket socket_bind — 给套接字绑定名字 socket_clear_error — 清除套接字或者最后的错误代码上的错误 socket_close — 关闭套接字资源 socket_cmsg_space — Calculate message buffer size socket_connect — 开启一个套接字连接 socket_create_listen — Opens a socket on port to accept connections socket_create_pair — Creates a pair of indistinguishable sockets and stores them in an array socket_create — 创建一个套接字(通讯节点) socket_get_option — Gets socket options for the socket socket_getopt — 别名 socket_get_option socket_getpeername — Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type socket_getsockname — Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type socket_import_stream — Import a stream socket_last_error — Returns the last error on the socket socket_listen — Listens for a connection on a socket socket_read — Reads a maximum of length bytes from a socket socket_recv — 从已连接的socket接收数据 socket_recvfrom — Receives data from a socket whether or not it is connection-oriented socket_recvmsg — Read a message socket_select — Runs the select() system call on the given arrays of sockets with a specified timeout socket_send — Sends data to a connected socket socket_sendmsg — Send a message socket_sendto — Sends a message to a socket, whether it is connected or not socket_set_block — Sets blocking mode on a socket resource socket_set_nonblock — Sets nonblocking mode for file descriptor fd socket_set_option — Sets socket options for the socket socket_setopt — 别名 socket_set_option socket_shutdown — Shuts down a socket for receiving, sending, or both socket_strerror — Return a string describing a socket error socket_write — Write to a socket
更多細節請查看PHP關於socket的官方手冊:http://php.net/manual/zh/book.sockets.php
一個簡單的TCP伺服器範例phptcpserver.php :
<?php $servsock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个socket if (FALSE === $servsock) { $errcode = socket_last_error(); fwrite(STDERR, "socket create fail: " . socket_strerror($errcode)); exit(-1); } if (!socket_bind($servsock, '127.0.0.1', 8888)) // 绑定ip地址及端口 { $errcode = socket_last_error(); fwrite(STDERR, "socket bind fail: " . socket_strerror($errcode)); exit(-1); } if (!socket_listen($servsock, 128)) // 允许多少个客户端来排队连接 { $errcode = socket_last_error(); fwrite(STDERR, "socket listen fail: " . socket_strerror($errcode)); exit(-1); } while (1) { $connsock = socket_accept($servsock); //响应客户端连接 if ($connsock) { socket_getpeername($connsock, $addr, $port); //获取连接过来的客户端ip地址和端口 echo "client connect server: ip = $addr, port = $port" . PHP_EOL; while (1) { $data = socket_read($connsock, 1024); //从客户端读取数据 if ($data === '') { //客户端关闭 socket_close($connsock); echo "client close" . PHP_EOL; break; } else { echo 'read from client:' . $data; $data = strtoupper($data); //小写转大写 socket_write($connsock, $data); //回写给客户端 } } } } socket_close($servsock);
啟動這個伺服器:
#
[root@localhost php]# php phptcpserver.php
#之後這個伺服器就一直阻塞在那裡,等待客戶端連接,我們可以用telnet命令來連接這個伺服器:
[root@localhost ~]# telnet 127.0.0.1 8888 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ajdjajksdjkaasda AJDJAJKSDJKAASDA 小明哈哈哈哈笑 小明哈哈哈哈笑 小明efsfsdfsdf了哈哈哈 小明EFSFSDFSDF了哈哈哈
伺服器端輸出:
#
[root@localhost php]# php phptcpserver.php client connect server: ip = 127.0.0.1, port = 50398 read from client:ajdjajksdjkaasda read from client:小明哈哈哈哈笑 read from client:小明efsfsdfsdf了哈哈哈
但其實這個TCP伺服器是有問題的,它一次只能處理一個客戶端的連接和數據傳輸,這是因為一個客戶端連接過來後,進程就去負責讀寫客戶端數據,當客戶端沒有傳輸數據時,tcp伺服器處於阻塞讀狀態,無法再去處理其他客戶端的連線請求了。
解決這個問題的一種方法就是採用多進程伺服器,每當一個客戶端連接過來,伺服器開一個子進程專門負責和該客戶端的資料傳輸,而父進程仍然監聽客戶端的連接,但是起進程的代價是昂貴的,這種多進程的機制顯然支撐不了高並發。
另一個解決方法是使用IO多路復用機制,使用php提供給我們的socket_select方法,它可以監聽多個socket,如果其中某個socket狀態發生了改變,例如從不可寫變成可寫,從不可讀變成可讀,這個方法就會返回,從而我們就可以去處理這個socket,處理客戶端的連接,讀寫操作等等。來看php文件中對該socket_select的介紹
#
socket_select — Runs the select() system call on the given arrays of sockets with a specified timeout 说明 int socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] ) socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket resources are watched. You do not need to pass every array to socket_select(). You can leave it out and use an empty array or NULL instead. Also do not forget that those arrays are passed by reference and will be modified after socket_select() returns. 返回值 On success socket_select() returns the number of socket resources contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens. On error FALSE is returned. The error code can be retrieved with socket_last_error().
大致翻譯下:
socket_select --- 在給定的幾組sockets數組上執行select() 系統調用,用一個特定的超時時間。
socket_select() 接受幾組sockets數組作為參數,並監聽它們改變狀態
這些基於BSD scokets 能夠識別這些socket資源數組實際上就是檔案描述符集合。
三個不同的socket資源陣列會同時被監聽。
這三個資源數組不是必傳的, 你可以用一個空數組或NULL作為參數,不要忘記這三個數組是以引用的方式傳遞的,在函數返回後,這些數組的值會被改變。
socket_select() 呼叫成功返回這三個數組中狀態改變的socket總數,如果設定了timeout,並且在timeout之內都沒有狀態改變,這個函數將返回0,出錯時回傳FALSE,可以用socket_last_error() 取得錯誤碼。
#使用socket_select() 最佳化之前phptcpserver.php 程式碼:
#
<?php $servsock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个socket if (FALSE === $servsock) { $errcode = socket_last_error(); fwrite(STDERR, "socket create fail: " . socket_strerror($errcode)); exit(-1); } if (!socket_bind($servsock, '127.0.0.1', 8888)) // 绑定ip地址及端口 { $errcode = socket_last_error(); fwrite(STDERR, "socket bind fail: " . socket_strerror($errcode)); exit(-1); } if (!socket_listen($servsock, 128)) // 允许多少个客户端来排队连接 { $errcode = socket_last_error(); fwrite(STDERR, "socket listen fail: " . socket_strerror($errcode)); exit(-1); } /* 要监听的三个sockets数组 */ $read_socks = array(); $write_socks = array(); $except_socks = NULL; // 注意 php 不支持直接将NULL作为引用传参,所以这里定义一个变量 $read_socks[] = $servsock; while (1) { /* 这两个数组会被改变,所以用两个临时变量 */ $tmp_reads = $read_socks; $tmp_writes = $write_socks; // int socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] ) $count = socket_select($tmp_reads, $tmp_writes, $except_socks, NULL); // timeout 传 NULL 会一直阻塞直到有结果返回 foreach ($tmp_reads as $read) { if ($read == $servsock) { /* 有新的客户端连接请求 */ $connsock = socket_accept($servsock); //响应客户端连接, 此时不会造成阻塞 if ($connsock) { socket_getpeername($connsock, $addr, $port); //获取远程客户端ip地址和端口 echo "client connect server: ip = $addr, port = $port" . PHP_EOL; // 把新的连接sokcet加入监听 $read_socks[] = $connsock; $write_socks[] = $connsock; } } else { /* 客户端传输数据 */ $data = socket_read($read, 1024); //从客户端读取数据, 此时一定会读到数组而不会产生阻塞 if ($data === '') { //移除对该 socket 监听 foreach ($read_socks as $key => $val) { if ($val == $read) unset($read_socks[$key]); } foreach ($write_socks as $key => $val) { if ($val == $read) unset($write_socks[$key]); } socket_close($read); echo "client close" . PHP_EOL; } else { socket_getpeername($read, $addr, $port); //获取远程客户端ip地址和端口 echo "read from client # $addr:$port # " . $data; $data = strtoupper($data); //小写转大写 if (in_array($read, $tmp_writes)) { //如果该客户端可写 把数据回写给客户端 socket_write($read, $data); } } } } } socket_close($servsock);
現在,這個TCP伺服器就可以支援多個客戶端同時連線了,測試下:
[root@localhost php]# php phptcpserver.php client connect server: ip = 127.0.0.1, port = 50404 read from client # 127.0.0.1:50404 # hello world client connect server: ip = 127.0.0.1, port = 50406 read from client # 127.0.0.1:50406 # hello PHP read from client # 127.0.0.1:50404 # 少小离家老大回 read from client # 127.0.0.1:50404 # 乡音无改鬓毛衰 read from client # 127.0.0.1:50406 # 老当益壮, read from client # 127.0.0.1:50406 # 宁移白首之心 client close client connect server: ip = 127.0.0.1, port = 50408
稍微修改上面的伺服器返回,返回一個HTTP回應頭和一個簡單的HTTP回應體,這樣就搖身一變成了一個最簡單的HTTP伺服器:
#
.... socket_getpeername($read, $addr, $port); //获取远程客户端ip地址和端口 echo "read from client # $addr:$port # " . $data; $response = "HTTP/1.1 200 OK\r\n"; $response .= "Server: phphttpserver\r\n"; $response .= "Content-Type: text/html\r\n"; $response .= "Content-Length: 3\r\n\r\n"; $response .= "ok\n"; if (in_array($read, $tmp_writes)) { //如果该客户端可写 把数据回写给客户端 socket_write($read, $response); socket_close($read); // 主动关闭客户端连接 //移除对该 socket 监听 foreach ($read_socks as $key => $val) { if ($val == $read) unset($read_socks[$key]); } foreach ($write_socks as $key => $val) { if ($val == $read) unset($write_socks[$key]); } } .....
重新啟動該伺服器,用curl模擬請求該http伺服器:
[root@localhost ~]# curl '127.0.0.1:8888' ok [root@localhost ~]# curl '127.0.0.1:8888' ok [root@localhost ~]# curl '127.0.0.1:8888' ok [root@localhost ~]# curl '127.0.0.1:8888' ok [root@localhost ~]# curl '127.0.0.1:8888' ok [root@localhost ~]#
伺服器端輸出:
#
client connect server: ip = 127.0.0.1, port = 50450 read from client # 127.0.0.1:50450 # GET / HTTP/1.1 User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 Host: 127.0.0.1:8888 Accept: */* client close client connect server: ip = 127.0.0.1, port = 50452 read from client # 127.0.0.1:50452 # GET / HTTP/1.1 User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 Host: 127.0.0.1:8888 Accept: */* client close client connect server: ip = 127.0.0.1, port = 50454 read from client # 127.0.0.1:50454 # GET / HTTP/1.1 User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 Host: 127.0.0.1:8888 Accept: */* client close client connect server: ip = 127.0.0.1, port = 50456 read from client # 127.0.0.1:50456 # GET / HTTP/1.1 User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 Host: 127.0.0.1:8888 Accept: */* client close
這樣一個高併發的HTTP伺服器就開發好了,用壓測軟體測試下並發能力:#
看到高達5000多的QPS,有沒有小激動呢^^。
PHP是世界上最好的語言 that's all !
以上是PHP實現系統程式設計之網路Socket及IO多路復用的詳細內容。更多資訊請關注PHP中文網其他相關文章!