久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP監(jiān)聽(tīng)Socket

瀏覽:5日期:2024-02-20 18:28:52

<?phpset_time_limit(10);

$commonProtocol = getprotobyname('tcp');$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);if ($socket) {$result = socket_bind($socket, 'localhost', 1337);if ($result) { $result = socket_listen($socket, 5); if ($result) { echo '監(jiān)聽(tīng)成功'; }}}else{echo '監(jiān)聽(tīng)失敗';}

do {if (($msgsock = socket_accept($socket))) { /* 發(fā)送提示信息給連接上來(lái)的用戶 */ $msg = '==========================================rn' . 'Welcome to the PHP Test Server. rnrn' . 'To quit, type 'quit'. rn' . 'To shut down the server type 'shutdown'.rn' . 'To get help message type 'help'.rn' . '==========================================rn' . 'php>';}

socket_write($msgsock, $msg, strlen($msg));

do { $buf = socket_read($msgsock, 2048, PHP_BINARY_READ);

if (false === $buf) { echo 'socket_read() failed: reason: ' . socket_strerror($result) . 'n'; break 2; } if (!$buf = trim($buf)) { continue; } /* 客戶端輸入quit命令時(shí)候關(guān)閉客戶端連接 */ if ($buf == 'q') { break; } /* 客戶端輸入shutdown命令時(shí)候服務(wù)端和客戶端都關(guān)閉 */ if ($buf == 'shutdown') { socket_close($msgsock); break 2; } /* 客戶端輸入help命令時(shí)候輸出幫助信息 */ if ($buf == 'h') { $msg = ' PHP Server Help Message rnrn' . ' To quit, type 'quit'. rn' . ' To shut down the server type 'shutdown'.rn' . ' To get help message type 'help'.rn' . 'php> '; socket_write($msgsock, $msg, strlen($msg)); continue; } /* 客戶端輸入命令不存在時(shí)提示信息 */ $talkback = 'PHP: unknow command '$buf'.rnphp> '; socket_write($msgsock, $talkback, strlen($talkback)); echo '$bufn';} while (true);socket_close($msgsock);}while (true);/* 關(guān)閉Socket連接 */socket_close($socket);?>

標(biāo)簽: PHP
主站蜘蛛池模板: 正蓝旗| 平遥县| 兰考县| 纳雍县| 石台县| 罗平县| 弥勒县| 乌拉特中旗| 衡阳市| 惠安县| 密山市| 文山县| 韶关市| 新河县| 红原县| 石景山区| 万宁市| 高邑县| 宁远县| 乌苏市| 华容县| 普兰店市| 筠连县| 望都县| 莱西市| 浠水县| 西藏| 昔阳县| 瓦房店市| 鄂托克前旗| 寿光市| 霍林郭勒市| 嫩江县| 香河县| 尚义县| 东安县| 额济纳旗| 区。| 栾川县| 赫章县| 岳阳县|