欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP Socket 編程

 更新時間:2010年04月09日 17:47:12   作者:  
讓我們以一個簡單的例子開始---一個接收輸入字符串,處理并返回這個字符串到客戶端的TCP服務(wù).
下面是相應(yīng)的代碼:
PHP 代碼:
復(fù)制代碼 代碼如下:

<?
// 設(shè)置一些基本的變量
$host = "192.168.1.99";
$port = 1234;
// 設(shè)置超時時間
set_time_limit(0);
// 創(chuàng)建一個Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
//綁定Socket到端口
$result = socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
// 開始監(jiān)聽鏈接
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
// accept incoming connections
// 另一個Socket來處理通信
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
// 獲得客戶端的輸入
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// 清空輸入字符串
$input = trim($input);
//處理客戶端輸入并返回結(jié)果
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// 關(guān)閉sockets
socket_close($spawn);
socket_close($socket);
?>

相關(guān)文章

最新評論