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自定义错误用法示例

本文实例讲述了PHP自定义错误用法。分享给大家供大家参考,具体如下: 自定义错误就是自己可以完全控制错误以及其提示内容 设定错误由自己定义的函数来处理 set_error_handl...

PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法

本文实例讲述了PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法。分享给大家供大家参考,具体如下: ICU(International Components fo...

php广告加载类用法实例

本文实例讲述了php广告加载类的用法,非常实用。分享给大家供大家参考。具体方法如下: 该php广告加载类,支持异步与同步加载。需要使用Jquery实现。 ADLoader.class.p...

PHP 程序员的调试技术小结

PHP 程序员的调试技术小结

本文介绍调试 PHP 应用程序的各种方法,包括在 Apache and PHP 中打开错误报告,以及通过在一个简单的 PHP 脚本中放置策略性的 print 语句,找到更困难的 bug...

php set_magic_quotes_runtime() 函数过时解决方法

把函数: set_magic_quotes_runtime($new_setting); 替换成: ini_set("magic_quotes_runtime", $new_settin...