Home > Backend Development > PHP Tutorial > PHP socket implements readInt and readString and interfaces with Java

PHP socket implements readInt and readString and interfaces with Java

WBOY
Release: 2016-07-29 08:57:46
Original
948 people have browsed it

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>
Copy after login

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>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the PHP socket implementation of readInt and readString and the docking with the Java end, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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