Home > Backend Development > PHP Tutorial > How to simulate socket and send data multiple times in PHP_PHP Tutorial

How to simulate socket and send data multiple times in PHP_PHP Tutorial

WBOY
Release: 2016-07-20 11:09:53
Original
995 people have browsed it

表四:Socket函数<br>函数名      描述<br>socket_accept()    接受一个Socket连接<br>socket_bind()     把socket绑定在一个IP地址和端口上<br>socket_clear_error()   清除socket的错误或者最后的错误代码<br>socket_close()     关闭一个socket资源<br>socket_connect()    开始一个socket连接<br>socket_create_listen()   在指定端口打开一个socket监听<br>socket_create_pair()   产生一对没有区别的socket到一个数组里<br>socket_create()    产生一个socket,相当于产生一个socket的数据结构<br>socket_get_option()    获取socket选项<br>socket_getpeername()   获取远程类似主机的ip地址<br>socket_getsockname()   获取本地socket的ip地址<br>socket_iovec_add()    添加一个新的向量到一个分散/聚合的数组<br>socket_iovec_alloc()   这个函数创建一个能够发送接收读写的iovec数据结构<br>socket_iovec_delete()   删除一个已经分配的iovec<br>socket_iovec_fetch()   返回指定的iovec资源的数据<br>socket_iovec_free()    释放一个iovec资源<br>socket_iovec_set()    设置iovec的数据新值<br>socket_last_error()    获取当前socket的最后错误代码<br>socket_listen()     监听由指定socket的所有连接<br>socket_read()     读取指定长度的数据<br>socket_readv()     读取从分散/聚合数组过来的数据<br>socket_recv()     从socket里结束数据到缓存<br>socket_recvfrom()    接受数据从指定的socket,如果没有指定则默认当前socket<br>socket_recvmsg()    从iovec里接受消息<br>socket_select()     多路选择<br>socket_send()     这个函数发送数据到已连接的socket<br>socket_sendmsg()    发送消息到socket<br>socket_sendto()    发送消息到指定地址的socket<br>socket_set_block()    在socket里设置为块模式<br>socket_set_nonblock()   socket里设置为非块模式<br>socket_set_option()    设置socket选项<br>socket_shutdown()    这个函数允许你关闭读、写、或者指定的socket<br>socket_strerror()    返回指定错误号的详细错误<br>socket_write()     写数据到socket缓存<br>socket_writev()    写数据到分散/聚合数组
Copy after login
<br>更多详细内容请查看:php教程er/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm">http://www.bkjia.com/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm
Copy after login
<?php <br />//post.php<br />function Post($host,$port)<br />{<br />    //$host="127.0.0.1";<br />    //建立连接<br />    $conn = fsockopen($host,$port);<br />    if (!$conn) <br />    {<br />        die("Con error");<br />    }<br />    //循环发送5次数据<br />    //<br />    for($i = 0;$i<5;$i++)<br />    {<br />        $data="user_name=admin".$i;<br />        WriteData($conn,$host,$data);<br />        echo $i."<br />";<br>    }<br>    <br>    fclose($conn);<br>}<br><br>function WriteData($conn,$host,$data)<br>{<br>    $header = "POST /test.php HTTP/1.1rn";<br>    $header.= "Host : {$host}rn";<br>    $header.= "Content-type: application/x-www-form-urlencodedrn";<br>    $header.= "Content-Length:".strlen($data)."rn";<br>    //Keep-Alive是关键<br>    $header.= "Connection: Keep-Alivernrn";    <br>    $header.= "{$data}rnrn";<br>    <br>    fwrite($conn,$header);<br>    <br>    //取结果<br>    //$result = '';<br>    //while(!feof($conn))<br>    //{<br>     //   $result .= fgets($conn,128);<br>    //}<br>    //return $result;<br>}<br><br>Post('127.0.0.1',80);<br>    <br>?>
Copy after login
<?php <br />    //test.php<br />    $fp = fopen('result.txt','a');<br />    $data = $_POST['user_name']." -- ". date('Y-m-d H:i:s')."rn";<br />    fwrite($fp,$data);<br />    fclose($fp);<br />?>
Copy after login
再模仿post实现用户登录
Copy after login
socket.php<br><blockquote><?php<br />/**<br />* @author macopad@qq.com<br />* 模拟socket发送post方式发送数据<br />* 发送文件为socket.php<br />* 接收数据为get_socket.php<br />* @var unknown_type<br />*/<br />$flag = 0;<br />//要post的数据<br />$argv = array(<br />&lsquo;username&rsquo;=>’macopad@qq.com’,<br>‘password’=>’macopad’<br>);<br>//构造要 post的字符串<br>$params = ”;<br>foreach ($argv as $key=>$value)<br>{<br>if ($flag!=0)<br>{<br>$params .= ”&”;<br>$flag = 1;<br>}<br>$params.= $key.”=”;<br>$params.= urlencode($value);<br>$flag = 1;<br>}<br>$length = strlen($params);//post的长度<br>//创建socket连接<br>$post = fsockopen($HTTP_SERVER_VARS["SERVER_ADDR"],80,$errno,$errstr,10) or exit($errstr.”—>”.$errno);<br>//构造post请求的头<br>$header = ”POST /guojinyong/test/get_socket.php HTTP/1.1rn”; //制定为 POST的方法提交数据 及要提交到的页面和协议类型<br>$header .= ”Host:”.$HTTP_SERVER_VARS["SERVER_ADDR"].”rn”;   //定义主机<br>$header .= ”Referer:http://”.$HTTP_SERVER_VARS["SERVER_ADDR"].”/guojinyong/test/socket.phprn”; //Referer信息,<br>$header .= ”Content-Type: application/x-www-form-urlencodedrn”; //说明这个请求为POST<br>$header .= ”Content-Length: ”.$length.”rn”; //提交的数据长度<br>$header .= ”Connection: Closernrn”;//关闭连接<br>$header .= $params.”rn”;//添加post的字符串<br>//发送post的数据<br>fputs($post,$header);<br>//接收get_socket.php返回的数据并打印出来<br>while(!feof($post))<br>{<br>echo fgets($post,1024);//从1024个字节之后开始获取<br>}<br>fclose($post); //关闭socket连接<br>?></blockquote><br>get_socket.php<br><blockquote>
<pre class="brush:php;toolbar:false"><?php<br />echo &rdquo;Set-Cookie:name=Macopad; expires=Fri 12-Nov-99 3:59:59 GMT&rdquo;;<br />$userName = &rdquo;";<br />$password = &rdquo;";<br />$userName = $_POST['username'];<br />$password = $_POST['password'];<br />echo &rdquo;<br>通过socket模拟程序发送数据!<br>”;<br>echo ”当前服务器是:”.$HTTP_SERVER_VARS["SERVER_ADDR"].”<br>”;<br>echo ”接受到的用户名是:” .$userName.”<br>接收到的密码是:”.$password;<br>显示结果<br>HTTP/1.1 200 OK Date: Wed, 14 Apr 2010 06:49:07 GMT Server: Apache X-Powered-By: PHP/5.2.5 Cache-Control: max-age=0 Expires: Wed, 14 Apr 2010 06:49:07 GMT Vary: Accept-Encoding Content-Length: 189 Connection: close Content-Type: text/html Set-Cookie:name=Macopad; expires=Fri 12-Nov-99 3:59:59 GMT<br>通过socket模拟程序发送数据!<br>当前服务器是:http://www.zhutiai.com
Copy after login
接受到的用户名是:macopad@qq.com<br>接收到的密码是:macopad
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444759.htmlTechArticleTable 4: Socket function function name description socket_accept() accepts a Socket connection socket_bind() binds the socket to a socket_clear_error() on IP address and port Clear socket errors or...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template