源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-03-05 14:05 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php模拟socket一次连接,多次发送数据的实现代码
[u]复制代码[/u] 代码如下:
<?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); ?>
[u]复制代码[/u] 代码如下:
<?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); ?>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部