Discuz教程网

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

[复制链接]
authicon dly 发表于 2011-9-5 19:58:45 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. //post.php
  3. function Post($host,$port)
  4. {
  5. //$host="127.0.0.1";
  6. //建立连接
  7. $conn = fsockopen($host,$port);
  8. if (!$conn)
  9. {
  10. die("Con error");
  11. }
  12. //循环发送5次数据
  13. //
  14. for($i = 0;$i<5;$i++)
  15. {
  16. $data="user_name=admin".$i;
  17. WriteData($conn,$host,$data);
  18. echo $i."<br />";
  19. }
  20. fclose($conn);
  21. }
  22. function WriteData($conn,$host,$data)
  23. {
  24. $header = "POST /test.php HTTP/1.1\r\n";
  25. $header.= "Host : {$host}\r\n";
  26. $header.= "Content-type: application/x-www-form-urlencoded\r\n";
  27. $header.= "Content-Length:".strlen($data)."\r\n";
  28. //Keep-Alive是关键
  29. $header.= "Connection: Keep-Alive\r\n\r\n";
  30. $header.= "{$data}\r\n\r\n";
  31. fwrite($conn,$header);
  32. //取结果
  33. //$result = '';
  34. //while(!feof($conn))
  35. //{
  36. // $result .= fgets($conn,128);
  37. //}
  38. //return $result;
  39. }
  40. Post('127.0.0.1',80);
  41. ?>
复制代码




代码如下:

  1. <?php
  2. //test.php
  3. $fp = fopen('result.txt','a');
  4. $data = $_POST['user_name']." -- ". date('Y-m-d H:i:s')."\r\n";
  5. fwrite($fp,$data);
  6. fclose($fp);
  7. ?>
复制代码








上一篇:简单的PHP写入数据库类代码分享
下一篇:PHP里的中文变量说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:12

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表