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

WBOY
Libérer: 2016-07-29 08:57:46
original
932 Les gens l'ont consulté

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>
Copier après la connexion

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>
Copier après la connexion
').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教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal