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

相关文章

浅析php中三个等号(===)和两个等号(==)的区别

先举个列子:比如你一个函数会返回这几种情况:1、大于0的数2、小于0的数3、等于0的数(也就是0啦)4、False(失败时)这时候如果你想捕获失败的情况,你就必须用===,而不能用==因...

PHP连续签到功能实现方法详解

本文实例讲述了PHP连续签到功能实现方法。分享给大家供大家参考,具体如下: require "./global.php"; $act = isset($_GET['act'])...

php中strtotime函数性能分析

最近在做一个游戏数据统计后台,最基础的功能是通过分析注册登录日志来展示用户数据。在公司内部测试,用户量很少,所以就没有发现什么性能问题。但是这两天一起放到真实的测试环境,用户量噌噌地就涌...

PHP设计模式之工厂模式(Factory Pattern)的讲解

面向对象编程中,工厂模式是我们最常用的实例化对象模式,工厂类就是一个专门用来创建其它对象的类,工厂类在多态性编程实践中是非常重要的。它允许动态替换类,修改配置,会使应用程序更加灵活。掌握...

PHP实时显示输出

//实时显示输出 ob_end_flush();//关闭缓存 //echo str_repeat(" ",256); //ie下 需要先发送256个字节 set_time_limit(0...