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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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."<br />";
}
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);
//}
//return $result;
}
Post('127.0.0.1',80);
?>

复制代码 代码如下:

<?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);
?>

相关文章

使用dump函数,给php加断点测试

复制代码 代码如下:function dump($var, $echo=true,$label=null, $strict=true){    $label...

PHP运行出现Notice : Use of undefined constant 的完美解决方案分享

Notice: Use of undefined constant ALL_PS - assumed 'ALL_PS' in E:\Server\vhosts\www.lvtao.net...

继续收藏一些PHP常用函数第1/2页

复制代码 代码如下: <? function GetIP() { //获取IP if ($_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERV...

又十个超级有用的PHP代码片段

好东西要大家一起分享,上次分享了十个,这次再来十个超级有用的PHP代码片段。 1. 发送短信 调用 TextMagic API。 // Include the TextMagic P...

PHP提高编程效率的20个要点

用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注...