网络通讯常用的有2种:行通讯和包体通讯,也就是常说的文本通讯和二进制通讯。文章分别在两种方式上实现erlang与c++通讯。
一、erlang 与 c++ 行通讯
行通讯是一种文本通讯,格式如“cmd args\n”,比较简单,erlang接收数据也比较简单,可以设置参数{packet, line},缺点是表示方式单一,很难表示结构数据。
1、erlang 代码(server.erl):
- -module(server).
- -export([start/0]).
- -define( PORT, 5678).
- %% 启动服务并接受客户端的连接
- start() ->
- {ok, LSock} = gen_tcp:listen(?PORT, [binary, {packet, line},{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, Data} ->
- io:format("recv line data: ~p~n", [Data]),
- gen_tcp:send(ASock, Data),
- server_loop(ASock);
- {error, _} ->
- {ok, recv_error}
- end.
2、c++ 代码(test.cpp):
- #include <winsock2.h>
- #include <iostream>
- #pragma comment(lib, "ws2_32.lib")
- int main(int argc, char* argv[])
- {
- WSADATA wsaData;
- WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
- SOCKET sc = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, NULL);
- SOCKADDR_IN addr;
- int len=sizeof(addr);
- addr.sin_family=AF_INET;
- addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- addr.sin_port = htons(5678);
- connect(sc, (struct sockaddr *)&addr, len);
- char buff[1024];
- memset(buff, 0, 1024);
- char data[] = "aaaaaa\n";
- //发送数据
- printf("send data: %s \n", data);
- send(sc, data, sizeof(data), 0);
- //接收数据
- recv(sc, buff, 1024, 0);
- printf("recv data: %s \n", buff);
- closesocket(sc);
- WSACleanup();
- getchar();
- return 0;
- }
包体通讯是二进制通讯,目前主要有结构体,json,asn等等,最简单还可以这样定义:2个字节cmd + 2个字节字符长度 + 字符内容。
下面以结构体包体通讯示例:
1、erlang代码(server.erl):
- -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, <<Len:?USHORT,Cmd:?USHORT,Contain:4/binary-unit:8,_Rest/binary>> = A} ->
- %% _Rest 匹配C++字符串后面的结束符/0
- 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.
- #include <winsock2.h>
- #include <iostream>
- #pragma comment(lib, "ws2_32.lib")
- struct data
- {
- unsigned short int len;
- unsigned short int cmd;
- char content[5] ;
- };
- int main(int argc, char* argv[])
- {
- WSADATA wsaData;
- WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
- SOCKET sc = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, NULL);
- SOCKADDR_IN addr;
- int len=sizeof(addr);
- addr.sin_family=AF_INET;
- addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- addr.sin_port = htons(5678);
- connect(sc, (struct sockaddr *)&addr, len);
- char buff[1024];
- memset(buff, 0, 1024);
- struct data pdata = {4, 1001, "test"};
- //发送数据
- printf("send data: %d %d %s \n", pdata.len, pdata.cmd, pdata.content);
- send(sc, (char *)&pdata, sizeof(pdata), 0);
- //接收数据
- recv(sc, buff, 1024, 0);
- struct data * pdata_get = (struct data *) buff;
- printf("recv data: %d %s \n", pdata_get->cmd, pdata_get->content);
- closesocket(sc);
- WSACleanup();
- getchar();
- return 0;