phpcocket例項
window下,開啟命令視窗,分別執行以下兩行命令:
# php.exe e:/wwwroot/server.php
# php.exe e:/wwwroot/server.php
以下程式碼來自php官方手冊
server.php:
<?php
//The Server
error_reporting(E_ALL);
$address = "127.0.0.1";
$port = "10000";
/* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock, $address, $port);
socket_listen($mysock, 5);
$client = socket_accept($mysock);
echo "Server started, accepting connections...
";
$i = 0;
while (true == true)
{
$i++;
echo "Sending $i to client.
";
socket_write($client, $i, strlen($i));
$input = socket_read($client, 2048);
echo "Response from client is: $input
";
sleep(5);
}
echo "Closing sockets...";
socket_close($client);
socket_close($mysock);
client.php
<?php
//The Client
error_reporting(E_ALL);
$address = "127.0.0.1";
$port = 10000;
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
} else {
echo "socket successfully created.
";
}
echo "Attempting to connect to `$address` on port `$port`...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
} else {
echo "successfully connected to $address.
";
}
$i = 0;
while (true == true)
{
$i++;
echo "Sending $i to server.
";
socket_write($socket, $i, strlen($i));
$input = socket_read($socket, 2048);
echo "Response from server is: $input
";
sleep(5);
}
echo "Closing socket...";
socket_close($socket);
相關文章
- 單例項刪除ASM例項單例ASM
- php例項化物件的例項方法PHP物件
- ruby中的類例項變數和例項的例項變數變數
- 2 Day DBA-管理Oracle例項-Oracle例項和例項管理概覽Oracle
- jQuery 例項jQuery
- OkHttpClient例項HTTPclient
- unittest例項
- RAC 例項 遷移到 單例項 -- 使用RMAN Duplicate單例
- UML用例圖例項解析
- js監聽鍵盤事件程式碼例項例項JS事件
- 演示中斷處理的例項(例項六)(轉)
- SQLMAP 例項COOKBOOKSQL
- ORM 例項教程ORM
- Java例項教程Java
- java介面例項Java
- YACC 例項分析
- Pull解析例項
- 管理 ASM 例項ASM
- 停止ASM例項ASM
- jQueryAjax例項演示jQuery
- 管理ORACLE例項Oracle
- 例項和物件物件
- Ant打包例項
- SQL TOP 例項SQL
- 策略模式例項模式
- pinctrl使用例項
- 類和例項
- Activiti的流程例項【ProcessInstance】與執行例項【Execution】
- JavaScript單例模式概念與例項JavaScript單例模式
- 單例模式 - 只有一個例項單例模式
- 單例與單例項之爭單例
- jQuery選項卡例項程式碼jQuery
- 如何將獨立例項轉換成叢集例項EU
- 搜尋框關鍵字智慧匹配例項程式碼例項
- RDS按量付費例項、只讀例項、OPENAPI正式釋出API
- 如何區分例項化網格中的每個例項
- tunna工具使用例項
- 4.1. Oracle例項Oracle