<?php
set_time_limit(0);
ob_implicit_flush();
$host = "127.0.0.1";
$port = 12390;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or exit("创建失败");
socket_set_block($socket) or exit("设置阻塞模式失败");
socket_bind($socket, $host, $port) or exit("监听失败");
socket_listen($socket, 4) or exit("设置最大监听数失败");
echo "开始监听::$host:$port\n";
while ($data = trim(fgets(STDIN))) {
$msgsock = socket_accept($socket);
socket_write($msgsock, $data, strlen($data));
while($buf = socket_read($msgsock, 2048)){
echo "从客户端接收到消息:$buf".PHP_EOL;
}
socket_close($msgsock);
// socket_close($msgsock);
// $buf = socket_read($msgsock,1024);
// echo "从客户端接收数据:$buf".PHP_EOL;
// socket_close($msgsock);
// continue;
}
socket_close($socket);
<?php
set_time_limit(0);
$host = "127.0.0.1";
$port = 12390;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or exit("创建失败");
echo "连接到::$host:$port...\n";
$result = socket_connect($socket, $host, $port) or exit("连接服务器失败");
while($buf = socket_read($socket, 2048)){
echo "从服务端接收到消息:$buf".PHP_EOL;
while($data = trim(fgets(STDIN))){
socket_write($socket, $data, strlen($data));
}
}
socket_close($socket);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。