ホームページ > バックエンド開発 > PHPチュートリアル > PHP ソケットがサーバー テンプレートに接続する、ソケット サーバー_PHP チュートリアル

PHP ソケットがサーバー テンプレートに接続する、ソケット サーバー_PHP チュートリアル

WBOY
リリース: 2016-07-13 10:18:48
オリジナル
785 人が閲覧しました

PHPのソケットはサーバーテンプレートに接続されており、ソケットサーバー

新しいフレームワークを整理していると、キャッシュされたデータの一部には外部インターフェースアクセスが必要で、より便利なのは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> $buffer
  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><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>@socket_read
  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>$socket
  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>9
  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> PHP_NORMAL_READ
  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. $crcCode
  27. <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><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><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><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>=
  28. (<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>ord
  29. (<span class="pln"> socket_close </span><span class="pun">(</span><span class="pln"> $socket </span><span class="pun">);</span>$buffer
  30. [<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>7
  31. ])<8🎜)+🎜ord🎜🎜(🎜$バッファ🎜🎜[🎜8🎜🎜]);🎜🎜🎜 🎜🎜 $len 🎜🎜=🎜 strlen🎜🎜(🎜$packet🎜🎜);🎜🎜🎜 🎜🎜 $newpacket 🎜🎜=🎜 CRC16🎜🎜::🎜encode🎜🎜(🎜$packet🎜🎜,🎜 $crcCode🎜🎜,🎜4🎜🎜);🎜🎜🎜 🎜🎜ソケット送信 🎜🎜(🎜 $socket🎜🎜,🎜 $newpacket🎜🎜,🎜 $len🎜🎜,🎜0🎜🎜);🎜🎜🎜 🎜🎜//等受付待ち🎜🎜🎜 🎜🎜 $head 🎜🎜=🎜@socket_read🎜🎜(🎜$socket🎜🎜,🎜4🎜🎜,🎜PHP_NORMAL_READ🎜🎜);🎜🎜🎜 🎜🎜 $len 🎜🎜=🎜🎜(🎜ord🎜🎜(🎜$head🎜🎜[🎜0🎜🎜])<<🎜24🎜🎜)+(🎜ord🎜🎜(🎜 $head🎜🎜[🎜1🎜🎜])<<🎜16🎜🎜)+(🎜ord🎜🎜 (🎜$head🎜🎜[🎜2🎜🎜])<<🎜8🎜🎜)+🎜ord🎜🎜(🎜$head🎜🎜[🎜3🎜🎜]);🎜🎜🎜 🎜🎜 $content 🎜🎜=🎜@socket_read🎜🎜(🎜$socket🎜🎜,🎜$len🎜🎜-🎜4🎜🎜,🎜PHP_NORMAL_READ🎜🎜);🎜🎜🎜 🎜🎜ソケット_クローズ 🎜🎜(🎜 $socket 🎜🎜);🎜🎜🎜 🎜🎜return🎜 substr🎜🎜(🎜$content🎜🎜,🎜3🎜🎜 );🎜🎜🎜 🎜🎜🎜}
  32. <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>
  33. public
  34. static<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>function
  35. packet
  36. <span class="pln"> $str </span><span class="pun">=</span><span class="str">''</span><span class="pun">;</span>(
  37. $group
  38. <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>,
  39. $cmd
  40. <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>,
  41. $message
  42. <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>){
  43. <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>
  44. $size <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>
  45. =
  46. strlen<span class="pln"> $str </span><span class="pun">.=</span><span class="pln"> $message</span><span class="pun">;</span>
  47. (
  48. $message<span class="kwd">return</span><span class="pln"> $str</span><span class="pun">;</span>
  49. )+
  50. 8<span class="pun">}</span>
  51. ;
  52. <span class="com">//写进2个byte的数据</span> $str
  53. =<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>''
  54. ;<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>
  55. <span class="pun">}</span>
  56. $str
  57. <span class="com">//写进4个byte的数据</span>.=
  58. self
  59. <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>::
  60. writeInt
  61. <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>(
  62. $size
  63. <span class="pun">}</span>);
  64. <span class="com">//写进1个byte的数据</span>
  65. $str <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>
  66. .=
  67. self<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>
  68. ::
  69. writeByte<span class="pun">(🎜<span class="lit ">0🎜🎜);🎜🎜🎜 🎜🎜<span class="pln"> $str 🎜🎜.=🎜<span class="kwd">self🎜🎜::🎜<span class="pln">writeByte🎜🎜(🎜<span class="pln ">$グループ🎜🎜);🎜🎜🎜 🎜🎜<span class="pln"> $str 🎜🎜.=🎜<span class="kwd">self🎜🎜::🎜<span class="pln">writeByte🎜🎜(🎜<span class="pln ">$cmd🎜🎜);🎜🎜🎜 🎜🎜<span class="pln"> $str 🎜🎜.=🎜<span class="kwd">self🎜🎜::🎜<span class="pln">writeByte🎜🎜(🎜<span class="lit ">1🎜🎜);🎜🎜🎜 🎜🎜<span class="pln"> $str 🎜🎜.=🎜<span class="pln"> $message🎜🎜;🎜🎜🎜 🎜🎜<span class="kwd">戻る🎜<span class="pln"> $str🎜🎜;🎜🎜🎜 🎜🎜🎜}🎜🎜🎜 🎜🎜🎜🎜 🎜🎜<span class="com">//写进2バイトのデータ🎜🎜🎜 🎜🎜<span class="kwd">private🎜<span class="kwd">static🎜<span class="kwd">function🎜<span class="pln"> writeShort🎜🎜(🎜<span class=" pln">$s🎜🎜)🎜🎜{🎜🎜🎜 🎜🎜<span class="kwd">返品🎜<span class="pln"> パック 🎜🎜(🎜<span class="str">"n"🎜🎜,🎜<span class="pln"> $s 🎜🎜);🎜🎜🎜 🎜🎜🎜}🎜🎜🎜 🎜🎜<span class="com">//写进4バイトのデータ🎜🎜🎜 🎜🎜<span class="kwd">private🎜<span class="kwd">static🎜<span class="kwd">function🎜<span class="pln"> writeInt🎜🎜(🎜<span class=" pln">$N🎜🎜)🎜🎜{🎜🎜🎜 🎜🎜<span class="kwd">返品🎜<span class="pln"> パック 🎜🎜(🎜<span class="str">"N"🎜🎜,🎜<span class="pln"> $N 🎜🎜);🎜🎜🎜 🎜🎜🎜}🎜🎜🎜 🎜🎜<span class="com">//写进1バイトのデータ🎜🎜🎜 🎜🎜<span class="kwd">プライベート🎜<span class="kwd">static🎜<span class="kwd">function🎜<span class="pln"> writeByte🎜🎜(🎜<span class=" pln">$b🎜🎜)🎜🎜{🎜🎜🎜 🎜🎜<span class="kwd">返品🎜<span class="pln"> パック 🎜🎜(🎜<span class="str">"c"🎜🎜,🎜<span class="pln"> $b 🎜🎜);🎜🎜🎜 🎜🎜🎜}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
  70. }<span class="pun">}</span>

個人サーバーに接続した後、crcCode 検証コードが割り当てられ、送信されるメッセージは crc16 で暗号化される必要があるため (実際には非常に単純ですが、謎めいたままにしておきます)、最初に crcCode を取得するために固定長が受け入れられるのを待ちます。 、リクエストを送信し、パケットが受け入れられて返されるのを待ちます。

Wiz Notes (Wiz) より

PHP の Socket はサーバーとクライアントの間でどのように通信しますか?


サーバーはデータバッファを提供し、ユーザー識別メカニズムを提供します。これは、両方のチャット ユーザーに対応する情報を送信するために使用されます。
ユーザー間の通信は、実際にはユーザーがサーバーと通信しているようです。
私の少しの理解

phpソケットに接続できません


ポートを確認してください

http://www.bkjia.com/PHPjc/880092.htmlwww.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/880092.html技術記事 PHP のソケットはサーバー テンプレートに接続されています。ソケット サーバーが新しいフレームワークを整理していたときに、一部のキャッシュされたデータには外部インターフェイスへのアクセスが必要であることが判明しました。より便利なのは PHP インターフェイスです...
🎜
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート