Socket communication between Php and Erlang

Source: Internet
Author: User
Socket communication between Php and Erlang ??

Generally, there are two common network communication methods: 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:

-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 the 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) of {OK, < > = A}-> io: format ("recv data :~ P ~ P ~ P ~ N ", [Len, Cmd, Contain]), % will receive the data sent back to the client gen_tcp: send (ASock, A), server_loop (ASock); {OK, data}-> io: format ("recv unformated data :~ P ~ N ", [Data]), server_loop (ASock); {error, _}-> {OK, recv_error} end.

Php code:

 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.

How to add magic method__ invoke in PHP 5.3
PHP has added a magic method named _ invoke since version 5.3. using this method, you can directly call an object after creating an instance. Example: classtestClass {publicfunction _ invoke {prin

PHP and JQueryJcrop for image cropping
We often see some websites with the image cutting function. you may think this function is gorgeous and mysterious! However, the jquery. Jcrop. min. plug-in for image cropping is introduced today.

PHP automatically redirects to the corresponding page based on the device type
With the popularization of mobile devices, Internet access is more convenient than in the past. For Android smartphones, iPhone/iPad and other mobile terminals, many websites have successively launched mobile devices for computers and such mobile phones.

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.