首页 >php教程 >php手册 >PHP的socket连接到服务端模版,socket服务端

PHP的socket连接到服务端模版,socket服务端

WBOY
WBOY原创
2016-06-13 09:25:171383浏览

PHP的socket连接到服务端模版,socket服务端

在整理新框架的时候,发现对于一些缓存数据,需要对外的接口访问,而比较方便的是php的接口,所以临时研究了下php如何连接java服务端。

先贴上代码:
  1. <span class="pun"></span><span class="pln">php</span>
  2. <span class="pln">require_once </span><span class="str">'CRC16.php'</span><span class="pun">;</span>
  3. <span class="com">/*-----------------------------</span>
  4. <span class="com"> | 向服务器发送数据包</span>
  5. <span class="com"> ------------------------------*/</span>
  6. <span class="kwd">class</span><span class="typ">Server</span><span class="pun">{</span>
  7. <span class="com">//发送数据包</span>
  8. <span class="kwd">public</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> sendPacket</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">,</span><span class="pln"> $host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">)</span><span class="pun">{</span>
  9. <span class="pln"> $protocol </span><span class="pun">=</span><span class="str">'tcp'</span><span class="pun">;</span>
  10. <span class="pln"> $get_prot </span><span class="pun">=</span><span class="pln"> getprotobyname </span><span class="pun">(</span><span class="pln"> $protocol </span><span class="pun">);</span>
  11. <span class="com">//创建socket</span>
  12. <span class="pln"> $socket </span><span class="pun">=</span><span class="pln"> socket_create </span><span class="pun">(</span><span class="pln"> AF_INET</span><span class="pun">,</span><span class="pln"> SOCK_STREAM</span><span class="pun">,</span><span class="pln"> $get_prot </span><span class="pun">);</span>
  13. <span class="com">//建立连接</span>
  14. <span class="pln"> $conn </span><span class="pun">=</span><span class="pln"> socket_connect </span><span class="pun">(</span><span class="pln"> $socket</span><span class="pun">,</span><span class="pln"> $host</span><span class="pun">,</span><span class="pln"> $port </span><span class="pun">);</span>
  15. <span class="kwd">if</span><span class="pun">(!</span><span class="pln">$conn</span><span class="pun">){</span>
  16. <span class="pln"> socket_close</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">);</span>
  17. <span class="kwd">exit</span><span class="pun">(</span><span class="str">"socket connect failed!"</span><span class="pun">);</span>
  18. <span class="pun">}</span>
  19. <span class="pln"> $buffer </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="lit">9</span><span class="pun">,</span><span class="pln"> PHP_NORMAL_READ</span><span class="pun">);</span>
  20. <span class="pln"> $crcCode </span><span class="pun">=</span><span class="pun">(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$buffer</span><span class="pun">[</span><span class="lit">7</span><span class="pun">])<span class="lit">8</span><span class="pun">)+</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$buffer</span><span class="pun">[</span><span class="lit">8</span><span class="pun">]);</span></span>
  21. <span class="pln"> $len </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">);</span>
  22. <span class="pln"> $newpacket </span><span class="pun">=</span><span class="pln"> CRC16</span><span class="pun">::</span><span class="pln">encode</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">,</span><span class="pln"> $crcCode</span><span class="pun">,</span><span class="lit">4</span><span class="pun">);</span>
  23. <span class="pln"> socket_send </span><span class="pun">(</span><span class="pln"> $socket</span><span class="pun">,</span><span class="pln"> $newpacket</span><span class="pun">,</span><span class="pln"> $len</span><span class="pun">,</span><span class="lit">0</span><span class="pun">);</span>
  24. <span class="com">//等待接受</span>
  25. <span class="pln"> $head </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="pln">PHP_NORMAL_READ</span><span class="pun">);</span>
  26. <span class="pln"> $len </span><span class="pun">=</span><span class="pun">(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])<span class="lit">24</span><span class="pun">)+(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">1</span><span class="pun">])<span class="lit">16</span><span class="pun">)+(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">2</span><span class="pun">])<span class="lit">8</span><span class="pun">)+</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]);</span></span></span></span>
  27. <span class="pln"> $content </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="pln">$len</span><span class="pun">-</span><span class="lit">4</span><span class="pun">,</span><span class="pln">PHP_NORMAL_READ</span><span class="pun">);</span>
  28. <span class="pln"> socket_close </span><span class="pun">(</span><span class="pln"> $socket </span><span class="pun">);</span>
  29. <span class="kwd">return</span><span class="pln"> substr</span><span class="pun">(</span><span class="pln">$content</span><span class="pun">,</span><span class="lit">3</span><span class="pun">);</span>
  30. <span class="pun">}</span>
  31. <span class="kwd">public</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> packet</span><span class="pun">(</span><span class="pln">$group</span><span class="pun">,</span><span class="pln">$cmd</span><span class="pun">,</span><span class="pln">$message</span><span class="pun">){</span>
  32. <span class="pln"> $size </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="pln">$message</span><span class="pun">)+</span><span class="lit">8</span><span class="pun">;</span>
  33. <span class="pln"> $str </span><span class="pun">=</span><span class="str">''</span><span class="pun">;</span>
  34. <span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeInt</span><span class="pun">(</span><span class="pln">$size</span><span class="pun">);</span>
  35. <span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span>
  36. <span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="pln">$group</span><span class="pun">);</span>
  37. <span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="pln">$cmd</span><span class="pun">);</span>
  38. <span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
  39. <span class="pln"> $str </span><span class="pun">.=</span><span class="pln"> $message</span><span class="pun">;</span>
  40. <span class="kwd">return</span><span class="pln"> $str</span><span class="pun">;</span>
  41. <span class="pun">}</span>
  42. <span class="com">//写进2个byte的数据</span>
  43. <span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeShort</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">)</span><span class="pun">{</span>
  44. <span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"n"</span><span class="pun">,</span><span class="pln"> $s </span><span class="pun">);</span>
  45. <span class="pun">}</span>
  46. <span class="com">//写进4个byte的数据</span>
  47. <span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeInt</span><span class="pun">(</span><span class="pln">$N</span><span class="pun">)</span><span class="pun">{</span>
  48. <span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"N"</span><span class="pun">,</span><span class="pln"> $N </span><span class="pun">);</span>
  49. <span class="pun">}</span>
  50. <span class="com">//写进1个byte的数据</span>
  51. <span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeByte</span><span class="pun">(</span><span class="pln">$b</span><span class="pun">)</span><span class="pun">{</span>
  52. <span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"c"</span><span class="pun">,</span><span class="pln"> $b </span><span class="pun">);</span>
  53. <span class="pun">}</span>
  54. <span class="pun">}</span>
因为个人服务端连接之后,会分配一个crcCode校验码,发送的消息需要进行crc16的加密(其实很简单的,保持点神秘感),所以先等待接受固定长度来获得crcCode,然后发送请求后,等待接受数据包,并返回。



来自为知笔记(Wiz)



PHP的Socket怎进行服务端与客户端的相互通信?

服务器提供数据缓冲区,并提供一种用户识别机制。以此来给聊天用户双方发送相应的信息。
看似用户与用户之间的通信实质上是用户在与服务器通信。
我的一点理解
 

php socket连接不上

查一下端口把
 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn