The socket communication between PHP and Erlang

Source: Internet
Author: User

??

Generally speaking, there are 2 kinds of methods used in network communication: Text communication and binary communication.

Text communication between PHP and Erlang is simpler. There is no discussion here, this paper mainly discusses the implementation of binary communication between PHP and Erlang. The implementation process is as follows:

Erlang-Side code:

-module(server).-export([start/0]).

-define (UINT, /unsigned-little-integer).-define (INT,/signed-little-integer ).-define ( USHORT, /unsigned-little-integer).-define (short,/signed-little-integer ).-define (UBYTE, 8/unsigned-little-integer).-define (BYTE, 8/signed-little-integer).

-define (PORT, 5678).

Percent start 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) of{OK, <<Len:? USHORT,CMD:? USHORT,contain:4/binary-Unit:8>> = A} - io: Format ("recv data: ~p ~p ~p~n", [Len, CMD, contain]), percent will receive the data sent back to the clientgen_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-Side code:

<?php$timeout=3;//Timeout time: 3 seconds$fp= Fsockopen ("tcp://127.0.0.1",5678,$errno,$errstr,$timeout/ * Connection Timeout time * /);if(!$fp) {Echo "$errstr ($errno) <br/>n";}Else{Stream_set_timeout ($fp,$timeout);//Remote data receive or send time-out $format="VVA4";$data= Pack ($format,4,10001,"ABCD");//$data are packaged as binary data in a certain formatFwrite$fp,$data);if(!feof ($fp)) {$rs= Fread ($fp,1024x768);//Read remote Data  if($rs) {$len= Strlen ($rs);//$len can get the length of the data. Used to calculate the length of content   //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);}?

>

Under normal circumstances, the PHP side will display the following content:

Array410001 [content] => abcd )

Some notes for communication:

Here is the PHP pack function and the unpack function:

Pack function: Package data into binary data in a certain format. The resulting data is close to the struct data of C + + + (c + + string terminator).

Unpack function: opposite to pack. Unpack the binary data.

The corresponding Erlang side. It is possible to use bit syntax to match binary data directly.

PHP 5.3 New Magic Method __invoke Overview
PHP has been adding a magic method called __invoke since version 5.3, using this method to create an instance. invokes the object directly.

For example, see the following demo sample: Classtestclass{publicfunction__invoke{prin

PHP combined with Jqueryjcrop to achieve a picture of practical examples of specific explanations
We often see images cut on some sites, and you might think this is a gorgeous feature. Amazing!

Today, however, a plug-in jquery dedicated to image trimming is introduced. Jcrop.min.

PHP implementation based on the device type of self-actively jump to the corresponding page method
with the popularity of mobile devices today, surfing the internet is more convenient than ever. For Android smartphones, Iphone/ipad and other mobile terminals, very many sites have been launched for the computer and such mobile phone

The socket communication between PHP and Erlang

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.