首页 > 资讯列表 > 编程/数据库 >> PHP

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

PHP 2014-12-06 10:21:05 转载来源: 网络整理/侵权必删

本文以示例的方式为大家讲解了php模拟socket一次连接,多次发送数据的实现代码,感兴趣的同学参考下。 <?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.1rn"; $hea

本文以示例的方式为大家讲解了php模拟socket一次连接,多次发送数据实现代码,感兴趣的同学参考下。


<?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.1rn";
$header.= "Host : {$host}rn";
$header.= "Content-type: application/x-www-form-urlencodedrn";
$header.= "Content-Length:".strlen($data)."rn";
//Keep-Alive是关键
$header.= "Connection: Keep-Alivernrn";
$header.= "{$data}rnrn";
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')."rn";
fwrite($fp,$data);
fclose($fp);
?>

 


标签: php 模拟 socket 一次 连接 多次 发送 数据 实现


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持