search
HomeBackend DevelopmentPHP Tutorial php 与 erlang 实现二进制通信

php 与 erlang 实现二进制通讯

网络通讯常用的有2种:文本通讯和二进制通讯。php与erlang之间文本通讯比较简单,这里不做讨论,主要讨论php与erlang实现二进制通讯。

通讯示例

erlang端代码:

-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() ->
  {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]),
      %%将接收到数据发送回客户端
      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端代码:

<?php $timeout = 3;
//超时时间:3秒

$fp = fsockopen("tcp://127.0.0.1", 5678, $errno, $errstr, $timeout/* 连接超时时间 */);
if (!$fp) {
  echo "$errstr ($errno)<br />\n";
} else {
  stream_set_timeout($fp, $timeout);
  //远程数据接收或发送超时时间

  $format = "vva4";
  $data = pack($format, 4, 10001, "abcd");
  //$data 按照一定格式被打包成二进制数据

  fwrite($fp, $data);

  if (!feof($fp)) {

    $rs = fread($fp, 1024);
    //读取远程数据
    if ($rs) {

      $len = strlen($rs);
      //$len 可以获取数据的长度,用以计算content的长度
      //在这个例子中,content 的长度为 4

      $format = "vlen/vcmd/a4content";
      $data = unpack($format, $rs);

      print_r($data);
    } else {
      echo "timeout!";
    }
  } else {
    echo "timeout!";
  }
  fclose($fp);
}
?>

正常情况下php端会显示以下内容:

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

通讯说明

这里用到的是php的pack函数和unpack函数

pack函数:将数据按照一定格式打包成二进制数据,生成的数据接近C/C++的结构体(C/C++字符串带结束符)。

unpack函数:与pack相反,对二进制数据进行解包。

而erlang端,直接用位语法来匹配二进制数据即可


Statement
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
Nginx timeout超时如何配置Nginx timeout超时如何配置May 12, 2023 pm 10:07 PM

keepalive_timeouthttp有一个keepalive模式,它告诉webserver在处理完一个请求后保持这个tcp连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。httpkeep-alive,網頁的每一個請求都是http(圖片,css等),而打開http請求是要先建立tcp連接,而如果一個頁面每個請求都要打開及關閉一個tcp連接就會做成資源的浪費.keepalive_timeout就是當一個http請求完成,其tcp連接會存留下

win11 clock watchdog timeout蓝屏怎么解决?win11 clock watchdog timeout蓝屏怎么解决?Feb 14, 2024 pm 04:00 PM

不少的用户在升级完win11系统后会出现蓝屏的现象,例如:clockwatchdogtimeout蓝屏,那么这要怎么解决?用户们可以看看更新驱动程序或者是检查过热问题等等来进行操作,下面就让本站来为用户们来仔细的介绍一下clockwatchdogtimeout蓝屏win11解决方法吧。clockwatchdogtimeout蓝屏win11解决方法1、更新驱动程序:更新CPU和主板驱动程序可能会解决问题。可以通过访问制造商的网站下载最新的驱动程序。2、检查过热问题:过热也可能是导致此错误的原因之一

在Vue应用中使用axios时出现“Error: timeout of xxxms exceeded”怎么办?在Vue应用中使用axios时出现“Error: timeout of xxxms exceeded”怎么办?Jun 24, 2023 pm 03:27 PM

在Vue应用中使用axios时出现“Error:timeoutofxxxmsexceeded”怎么办?随着互联网的快速发展,前端技术也在不断地更新迭代,Vue作为一种优秀的前端框架,近年来受到大家的欢迎。在Vue应用中,我们常常需要使用axios来进行网络请求,但是有时候会出现“Error:timeoutofxxxmsexceeded”的错误

define怎么定义多行宏define怎么定义多行宏Oct 11, 2023 pm 01:24 PM

define定义多行宏可以通过使用 `\` 将 `do { \ printf("%d\n", x); \ } while (0)` 分成了多行进行定义。在宏定义中,反斜杠 `\` 必须是宏定义的最后一个字符,且不能有空格或注释跟随。使用 `\` 进行续行时,注意保持代码的可读性,并确保每个行末都有 `\`。

在Vue应用中使用vue-resource时出现“Error: timeout of xxxms exceeded”怎么办?在Vue应用中使用vue-resource时出现“Error: timeout of xxxms exceeded”怎么办?Jun 24, 2023 pm 02:21 PM

在Vue应用开发中,使用vue-resource进行HTTP请求是常见的操作。尽管vue-resource提供了很多方便的功能,但有时我们会遇到“Error:timeoutofxxxmsexceeded”这样的错误提示。这种错误通常是因为请求超时而导致的。那么,在这种情况下,我们应该怎样解决这个问题呢?1.增加请求超时时间首先,我们可以通过增加请

探究PHP中define函数的重要性与作用探究PHP中define函数的重要性与作用Mar 19, 2024 pm 12:12 PM

PHP中define函数的重要性与作用1.define函数的基本介绍在PHP中,define函数是用来定义常量的关键函数,常量在程序运行过程中不会改变其值。利用define函数定义的常量,在整个脚本中均可被访问,具有全局性。2.define函数的语法define函数的基本语法如下:define("常量名称","常量值&qu

504 gateway timeout怎么解决504 gateway timeout怎么解决Nov 27, 2023 am 10:55 AM

504 gateway timeout的解决办法:1、检查服务器负载;2、优化查询和代码;3、增加超时限制;4、检查代理服务器;5、检查网络连接;6、使用负载均衡;7、监控和日志;8、故障排除;9、增加缓存;10、分析请求。解决该错误通常需要综合考虑多个因素,包括服务器性能、网络连接、代理服务器配置和应用程序优化等。

define怎么定义条件编译define怎么定义条件编译Oct 11, 2023 pm 01:20 PM

define定义条件编译可以使用 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif` 预处理指令来实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool