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

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

PHP監(jiān)聽Socket

瀏覽:3日期: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)聽成功'; }}}else{echo '監(jiān)聽失敗';}

do {if (($msgsock = socket_accept($socket))) { /* 發(fā)送提示信息給連接上來的用戶 */ $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命令時候關(guān)閉客戶端連接 */ if ($buf == 'q') { break; } /* 客戶端輸入shutdown命令時候服務(wù)端和客戶端都關(guān)閉 */ if ($buf == 'shutdown') { socket_close($msgsock); break 2; } /* 客戶端輸入help命令時候輸出幫助信息 */ 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; } /* 客戶端輸入命令不存在時提示信息 */ $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
主站蜘蛛池模板: 乾安县| 长春市| 独山县| 民勤县| 辽宁省| 云南省| 武汉市| 周口市| 洞口县| 台南县| 恩施市| 巧家县| 安远县| 论坛| 曲阜市| 湘乡市| 龙胜| 汉川市| 丹阳市| 高州市| 土默特右旗| 建水县| 原平市| 嘉祥县| 海南省| 兴义市| 巴彦县| 福安市| 南雄市| 南部县| 普宁市| 静乐县| 穆棱市| 苗栗县| 龙胜| 肇庆市| 砀山县| 西乌| 马边| 东莞市| 外汇|