首页 >后端开发 >php教程 >PHP socket实现 readInt 和 readString 以及和Java端对接

PHP socket实现 readInt 和 readString 以及和Java端对接

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-07-29 08:57:46958浏览

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