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

前端之家收集整理的这篇文章主要介绍了php模拟socket一次连接,多次发送数据的实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="74340" class="copybut" id="copybut74340" onclick="doCopy('code74340')"> 代码如下:

<div class="codebody" id="code74340">
<?PHP
//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,$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);
//}
//return $result;
}
Post('127.0.0.1',80);
?>

<div class="codetitle"><a style="CURSOR: pointer" data="66054" class="copybut" id="copybut66054" onclick="doCopy('code66054')"> 代码如下:
<div class="codebody" id="code66054">
<?PHP
//test.PHP
$fp = fopen('result.txt','a');
$data = $_POST['user_name']." -- ". date('Y-m-d H:i:s')."\r\n";
fwrite($fp,$data);
fclose($fp);
?>

原文链接:https://www.f2er.com/php/27726.html

猜你在找的PHP相关文章