首頁 >後端開發 >php教程 >PHP socket實作 readInt 和 readString 以及和Java端對接

PHP socket實作 readInt 和 readString 以及和Java端對接

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-07-29 08:57:46956瀏覽

java:

<code>DataOutputStream <span>out</span> = new DataOutputStream(new BufferedOutputStream(socket<span>.getOutputStream</span>()))<span>;</span><span>out</span><span>.writeInt</span>(<span>"REWQTY"</span><span>.getBytes</span>()<span>.length</span>)<span>;</span><span>out</span><span>.write</span>(<span>"REWQTY"</span><span>.getBytes</span>())<span>;</span><span>out</span><span>.close</span>()<span>;//php端是阻塞的,除非close</span></code>

php:

<code><span><span>function</span><span>readInt</span><span>(<span>$sock</span>)</span>
{</span><span>$buf</span> = socket_read(<span>$sock</span>,<span>4</span>);
   <span>if</span> (<span>$buf</span>){
       <span>return</span> unpack(<span>'N'</span>,<span>$buf</span>)[<span>1</span>];
   }<span>else</span>{
       <span>throw</span><span>new</span><span>Exception</span>();
   }
}

<span><span>function</span><span>readString</span><span>(<span>$socket</span>)</span> {</span><span>$n</span> = readInt(<span>$socket</span>);
  <span>return</span> socket_read(<span>$socket</span>, <span>$n</span>);
}

<span>echo</span> readString(<span>$sock</span>,<span>$l</span>);
<span>echo</span><span>"\n"</span>;</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了PHP socket實作 readInt 和 readString 以及和Java端對接,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn