手机
当前位置:查字典教程网 >编程开发 >php教程 >php模拟socket一次连接,多次发送数据的实现代码
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一次连接,多次发送数据的实现代码】相关文章:

php简单操作mysql数据库的类

利用js调用后台php进行数据处理原码

PHP如何透过ODBC来存取数据库

用PHP调用数据库的存贮过程!

php使用cookie实现记住用户名和密码实现代码

php中关于socket的系列函数总结

留言板翻页的实现详解

论坛头像随机变换代码

用PHP连接Oracle数据库

php curl请求信息和返回信息设置代码实例

精品推荐
分类导航