Home php教程 php手册 php模拟socket 多次发送数据的实现方法

php模拟socket 多次发送数据的实现方法

Jun 13, 2016 am 11:26 AM
php socket function send accomplish data method simulation of

表四: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    {<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>‘username’=>’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 ”Set-Cookie:name=Macopad; expires=Fri 12-Nov-99 3:59:59 GMT”;<br>$userName = ”";<br>$password = ”";<br>$userName = $_POST['username'];<br>$password = $_POST['password'];<br>echo ”<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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles