Php parses binary communication instances with erlang

Source: Internet
Author: User
This article describes how to parse binary communication instances between php and erlang. For more information, see the following two common methods of network communication: text communication and binary communication. It is relatively simple to implement text communication between php and erlang. we will not discuss it here. this article mainly discusses how php and erlang implement binary communication. The implementation steps are as follows:

Erlang code:

The code is as follows:

-Module (server ).
-Export ([start/0]).

-Define (UINT, 32/unsigned-little-integer ).
-Define (INT, 32/signed-little-integer ).
-Define (USHORT, 16/unsigned-little-integer ).
-Define (SHORT, 16/signed-little-integer ).
-Define (UBYTE, 8/unsigned-little-integer ).
-Define (BYTE, 8/signed-little-integer ).

-Define (PORT, 5678 ).

% Start the service and accept client connection
Start ()->
{OK, LSock} = gen_tcp: listen (? PORT, [binary, {packet, 0}, {active, false}]),
Io: format ("socket listen :~ P on ~ P ~ N ", [LSock ,? PORT]),
Accept (LSock ).

Accept (LSock)->
{OK, ASock} = gen_tcp: accept (LSock ),
Spawn (fun ()-> server_loop (ASock) end ),
Accept (LSock ).

Server_loop (ASock)->
Case gen_tcp: recv (ASock, 0)
{OK, < >=A}->
Io: format ("recv data :~ P ~ P ~ P ~ N ", [Len, Cmd, Contain]),
% The received data will be sent back to the client
Gen_tcp: send (ASock, ),
Server_loop (ASock );
{OK, Data}->
Io: format ("recv unformated data :~ P ~ N ", [Data]),
Server_loop (ASock );
{Error, _}->
{OK, recv_error}
End.

Php code:

<? Php $ timeout = 3; // timeout: 3 seconds $ fp = fsockopen ("tcp: // 127.0.0.1", 5678, $ errno, $ errstr, $ timeout/* connection timeout */); if (! $ Fp) {echo "$ errstr ($ errno)
\ N ";}else {stream_set_timeout ($ fp, $ timeout); // timeout value for remote data receiving or sending $ format =" vva4 "; $ data = pack ($ format, 4, 10001, "abcd"); // $ data is packaged into binary data fwrite ($ fp, $ data) in a certain format; if (! Feof ($ fp) {$ rs = fread ($ fp, 1024); // read remote data if ($ rs) {$ len = strlen ($ rs ); // $ len can obtain the data length to calculate the content length. // In this example, the content length is 4 $ format = "vlen/vcmd/a4content "; $ data = unpack ($ format, $ rs); print_r ($ data);} else {echo "timeout! ";}} Else {echo" timeout! ";}Fclose ($ fp) ;}?>

When the php client runs normally, the following content is displayed:

Array ( [len] => 4 [cmd] => 10001 [content] => abcd )

For communicationDescription:

Here we use the pack and unpack functions of php:

Pack function: pack data into binary data in a certain format. the generated data is close to the structure data of C/C ++ (C/C ++ string with an Terminator ).

Unpack function: Unlike pack, unpack binary data.

For the corresponding erlang end, you can directly use the bit syntax to match the binary data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.