Flash 与 Erlang 服务器通信
看来这是一个 erlang 问题。 我有这个代码来测试客户端发送数据,用 Actionscript 3 编写:
var socket:Socket=new Socket("localhost", 2345);
socket.addEventListener(Event.CONNECT, connected);
private function connected(event:Event):void {
socket.writeInt(12); //packet length, should be correct? 4 bytes each?
socket.writeInt(3);
socket.writeInt(6);
socket.writeInt(9);
socket.flush();
}
然后我有这个小型服务器,用 Erlang 编写:
start_nano_server() ->
{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 0},
{reuseaddr, true},
{active, true},
{packet_size, 128}]),
{ok, Socket} = gen_tcp:accept(Listen),
gen_tcp:close(Listen),
receive_data(Socket, []).
receive_data(Socket, SoFar) ->
receive
{tcp,Socket,Bin} ->
receive_data(Socket, [Bin|SoFar]);
{tcp_closed,Socket} ->
Bytes=list_to_binary(reverse(SoFar)),
io:format("~p~n",[Bytes])
end.
现在,无论我从客户端发送什么,我总是得到 [<<0 ,0,0,4,0,0,0,32>>]
作为响应。 我可以尝试直接向套接字写入字节而不是整数,我得到了同样的结果。 我可以写更多或更少的数据,但结果相同。 UTF 字符串结果相同。 即使指定“4”作为数据包标头长度,我也只会得到与 [<<0,0,0,32>>]
相同的一致结果。 我不明白我在这里做错了什么。
This is an erlang problem, it seems. I have this code to test the client sending data, written in Actionscript 3:
var socket:Socket=new Socket("localhost", 2345);
socket.addEventListener(Event.CONNECT, connected);
private function connected(event:Event):void {
socket.writeInt(12); //packet length, should be correct? 4 bytes each?
socket.writeInt(3);
socket.writeInt(6);
socket.writeInt(9);
socket.flush();
}
Then I have this small server, written in Erlang:
start_nano_server() ->
{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 0},
{reuseaddr, true},
{active, true},
{packet_size, 128}]),
{ok, Socket} = gen_tcp:accept(Listen),
gen_tcp:close(Listen),
receive_data(Socket, []).
receive_data(Socket, SoFar) ->
receive
{tcp,Socket,Bin} ->
receive_data(Socket, [Bin|SoFar]);
{tcp_closed,Socket} ->
Bytes=list_to_binary(reverse(SoFar)),
io:format("~p~n",[Bytes])
end.
Now, no matter what I send from the client, I ALWAYS get [<<0,0,0,4,0,0,0,32>>]
as the response. I can try writing bytes to the socket directly instead of ints, and I get the same thing. I can write more or less data, same result. UTF strings same result. Even when specifying "4" as the packet header length, I just get the same consistent result of [<<0,0,0,32>>]
instead. I don't understand what I'm doing wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是我们连接到 Flash 套接字的工作 Erlang 服务器的摘录...
它基于 Klacke 的 chargen 服务器示例:
http://erlang.org/examples/klacke_examples/chargen.erl
Here's an extract from our working Erlang server that connects to a Flash socket...
It is based on Klacke's chargen server example:
http://erlang.org/examples/klacke_examples/chargen.erl
在接收数据之前尝试不要关闭
Listen
。Try not closing
Listen
before receiving data.怀疑它更有可能是 Flash 问题 - 您的 Erlang 服务器代码与 Java 套接字客户端完美配合。
Suspect its more likely a Flash problem - your Erlang server code works perfectly with a Java socket client.
如果有人想知道答案,我将回答我自己的问题。
使用数据包嗅探器,我能够发现 Flash 确实发送了错误的数据包。 虽然我不知道数据包数据的实际意义是什么,但我知道问题出在哪里。 我想这是因为我在同一台机器上打开了Flash套接字和Erlang套接字 - 通常这不会成为问题,但我相信由于Flash在侦听套接字和发送套接字之间没有任何区别,因此它以某种方式与 Erlang 程序上的打开套接字发生冲突。
当您在 Flash 套接字中指定 Socket(host, port) 时,您不仅可以在该套接字上发送数据,而且还可以接收数据,因此这似乎是问题的根源。
我测试了在我的笔记本电脑上运行 Flash 程序和在我的 PC 上运行服务器,效果很好。
I am about to answer my own question in case anyone wants to know the answer.
Using a packet sniffer, I was able to find out that Flash was indeed sending the wrong packet. Although I don't know the significance of what the packet data actually was, I know what the problem was. I guess it was because I opened the Flash socket and the Erlang socket on the same machine - normally this wouldn't be a problem, but I believe that since Flash doesn't have any differenciation between listening sockets and sending sockets, that it somehow clashed with the open socket on the Erlang program.
When you specify Socket(host, port) in the Flash socket, not only are you able to send data on that socket, but you can receive it too, so that seemed to be the source of the problem.
I tested running the Flash program on my laptop and the server on my PC and it worked fine.