首頁  >  文章  >  後端開發  >  php模擬socket一次連線,多次傳送資料的實作程式碼

php模擬socket一次連線,多次傳送資料的實作程式碼

高洛峰
高洛峰原創
2016-11-30 11:34:291155瀏覽

 程式碼如下: 
//post.php 
function Post($host,$port) 

//$host="127.0.0.1"; 
//建立連線 = host,$port); 
if (!$conn) 

die("Con 錯誤"); 

//循環發送5次資料 
// 
for($i = 0;$i{ 
$data="user_name=admin".$i; 
WriteData($conn,$host,$data); 
echo $i。 "
"; 

fclose($conn); 

function WriteData($conn,$host,$data) 

$header = "POST /test,$data) 

$header = "POST /test,$data) 

$header = "POST /test.php HTTP/1.1.1]; $header.= "主機: {$host}rn"; 
$header.= "內容類型:application/x-www-form-urlencodedrn"; 
$header.= "內容長度:".strlen($data) ."rn"; 
//Keep-Alive 是關鍵
$header.= "Connection: Keep-Alivenrn"; 
$header.= "{$data}rnrn"; 
fwrite($conn,$header); 
//取得結果 
//$result = ''; 
//while(!feof($conn)) 
//{ 

// $result .= fgets($conn,128); 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜/ 🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn