php模拟socket一次连接,多次发送数据的实现代码

高洛峰
发布: 2023-03-01 12:20:01
原创
1154 人浏览过

 代码如下: 
//post.php
function Post($host,$port)
{
//$host="127.0.0.1";
//建立连接
$conn = fsockopen($host,$port);
if (!$conn)
{
die("Con error");
}
//循环发送5次数据
//
for($i = 0;$i<5;$i++)
{
$data="user_name=admin".$i;
WriteData($conn,$host,$data);
echo $i."
"; 

fclose($conn); 

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

$header = "POST /test.php HTTP/1.1\r\n"; 
$header.= "Host : {$host}\r\n"; 
$header.= "Content-type: application/x-www-form-urlencoded\r\n"; 
$header.= "Content-Length:".strlen($data)."\r\n"; 
//Keep-Alive是关键 
$header.= "Connection: Keep-Alive\r\n\r\n"; 
$header.= "{$data}\r\n\r\n"; 
fwrite($conn,$header); 
//取结果 
//$result = ''; 
//while(!feof($conn)) 
//{ 
// $result .= fgets($conn,128); 

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!