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);
相關文章
- php例項化物件的例項方法PHP物件
- unittest例項
- OkHttpClient例項HTTPclient
- jQuery 例項jQuery
- Activiti的流程例項【ProcessInstance】與執行例項【Execution】
- 策略模式例項模式
- Draggable 拖拽例項
- python例項1Python
- 類和例項
- pinctrl使用例項
- SQLMAP 例項COOKBOOKSQL
- Java例項教程Java
- expect使用例項
- 例項化list
- msfvenom使用例項
- ORM 例項教程ORM
- 雙層 for 例項
- python socket例項Python
- Proxy例項set()
- 如何區分例項化網格中的每個例項
- 如何將獨立例項轉換成叢集例項EU
- Java--例項化Java
- Makefile例項學習
- python鬧鐘例項Python
- SoapUI入門例項UI
- Oracle Far Sync例項Oracle
- python 類和例項Python
- TypeScript入門例項TypeScript
- 3.1.5 啟動例項
- 4.1. Oracle例項Oracle
- tunna工具使用例項
- Memory Dump利用例項
- 例項 靜態 類
- vue 列表渲染例項Vue
- python100例項Python
- 2.Vue例項Vue
- 開始例項化
- cluster例項運用
- redis sentinel哨兵 例項Redis