Home > php教程 > php手册 > body text

【博文推荐】php的Socket通信以及出现的错误(1)

WBOY
Release: 2016-06-13 09:07:27
Original
1105 people have browsed it

【博文推荐】php的Socket通信以及出现的错误(1)

现在的PYTHON很火,但是我们的PHP也不错!主要是看有没有去挖掘它,去在phper中开更多的脑洞,有时学习框架学累的时候,不妨写写脚本。或许能帮助自己更好的了解PHP。

 本博文出自Bkjia博客且行且欣赏博主,有任何问题请进入博主页面互动讨论!
博文地址: http://skytina.blog.51cto.com/6834539/1615463

这里我们就不阐述Socket套接字)是什么了,这里我们使用常见的C(client)/S(server)模式。作为服务器端,它的操作socket流程如下:

下面是Server.php的代码

<ol class="dp-c"><li class="alt"><span><span>#The script is used to listen the requests from client </span></span></li><li><span><span class="func">error_reporting</span><span>(E_ALL & ~E_NOTICE); </span></span></li><li class="alt"><span><span class="vars">$ip</span><span> = </span><span class="string">'127.0.0.1'</span><span>; </span></span></li><li><span><span class="vars">$port</span><span> = 1993; </span></span></li><li class="alt"><span><span class="vars">$sock</span><span> = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); </span></span></li><li><span><span class="keyword">if</span><span>(!</span><span class="vars">$sock</span><span>){ </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'In create process'</span><span>.socket_strerror(socket_last_error); </span></span></li><li><span>} </span></li><li class="alt"><span><span class="vars">$bind_ornot</span><span> = socket_bind(</span><span class="vars">$sock</span><span>,</span><span class="vars">$ip</span><span>,</span><span class="vars">$port</span><span>); </span></span></li><li><span><span class="keyword">if</span><span>(!</span><span class="vars">$bind_ornot</span><span>) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="func">echo</span><span> </span><span class="string">'In bind process:'</span><span>.socket_strerror(socket_last_error(</span><span class="vars">$sock</span><span>)); </span></span></li><li class="alt"><span><span class="keyword">return</span><span> 0; </span></span></li><li><span>} </span></li><li class="alt"><span><span class="keyword">if</span><span>(socket_listen(</span><span class="vars">$sock</span><span>,10)){ </span></span></li><li><span><span class="vars">$flag</span><span> = 1; </span></span></li><li class="alt"><span><span class="keyword">do</span><span>{ </span></span></li><li><span><span class="vars">$conn_socket</span><span> = socket_accept(</span><span class="vars">$sock</span><span>); </span></span></li><li class="alt"><span><span class="vars">$msg</span><span> = </span><span class="string">"Send Message:Hello,World"</span><span>; </span></span></li><li><span><span class="keyword">if</span><span>(socket_write(</span><span class="vars">$conn_socket</span><span>,</span><span class="vars">$msg</span><span>,</span><span class="func">strlen</span><span>(</span><span class="vars">$msg</span><span>))) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="func">echo</span><span> </span><span class="string">"第{$flag}次:发送成功\n"</span><span>; </span></span></li><li class="alt"><span>}; </span></li><li><span><span class="keyword">if</span><span>(</span><span class="vars">$flag</span><span> == 5) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="func">echo</span><span> </span><span class="string">"停止监听,发送完成"</span><span>; </span></span></li><li class="alt"><span><span class="keyword">break</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span><span class="vars">$flag</span><span>++; </span></span></li><li><span>socket_close(<span class="vars">$conn_socket</span><span>); </span></span></li><li class="alt"><span>}<span class="keyword">while</span><span>(True); </span></span></li><li><span>} </span></li><li class="alt"><span>?> </span></li></ol>
Copy after login

socket_create函数是用来创建一个socket资源,它有三个参数,第一个参数指的是协议族,其中AF_INET指的是使用ipv4的协议族,第二个参数是指使用的通信类型,SOCK_STREAM指的是流模式。流模式对应的是可靠连接,也就是tcp连接方式。第三个指的是选用哪一种连接方式,要是tcp就是用SOL_TCP.要是udp就使用SOL_UDP。对于socket_listen,第一个参数是要监听的套接字,第二个是可允许的队列数目,即允许有多少个人连接。但在使用前得先绑定一个ip地址和端口上面的php代码,监听本地环路地址‘127.0.0.1’的1993端口,只要有连接请求发送到1993端口,便与客户端建立tcp连接,之后便是发送信息到客户端,发送完消息之后便关闭连接。里面的flag为计数标志变量,当$flag为5的时候,停止建立连接。PS:socket_strerror(socket_last_error($sock))返回的是对应套接字产生错误的原因,编码是根据操作系统所使用的语言,我这里是windows的中文系统,所以返回的是gbk编码!接下来是client.php操作socket的流程。

下面是代码:

<ol class="dp-c"><li class="alt"><span><span class="func">error_reporting</span><span>(E_ALL & ~E_NOTICE); </span></span></li><li><span><span class="vars">$sock</span><span> = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$sock</span><span>){ </span></span></li><li><span><span class="keyword">if</span><span>(socket_connect(</span><span class="vars">$sock</span><span>,</span><span class="string">'127.0.0.1'</span><span>,1993)) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="keyword">while</span><span>((</span><span class="vars">$content</span><span> = socket_read(</span><span class="vars">$sock</span><span>,256))) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="func">echo</span><span> </span><span class="vars">$content</span><span>.</span><span class="string">"\n"</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="keyword">else</span><span> </span></span></li><li><span>{ </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'In connect process'</span><span>.socket_strerror(socket_last_error); </span></span></li><li><span>} </span></li><li class="alt"><span>} </span></li><li><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="func">echo</span><span> </span><span class="string">'In create process'</span><span>.socket_strerror(socket_last_error(</span><span class="vars">$sock</span><span>)); </span></span></li><li class="alt"><span>} </span></li><li><span>?> </span></li></ol>
Copy after login

上面的代码使用socket_connect连接到正在监听的服务器端口1993,连接后,通过socket_read从连接后的套接字读取内容!返回的由于是字符串类型,可以直接打印!之后我们先运行server.php,因为其负责监听指定端口和发送信息。倘若我们先运行client.php,那我们便会发现返回这样的错误!




Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template