- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Erlang server, Java client - TCP messages get split?
(3 个回答)
7年前关闭。
我正在使用 this 中的 socket_server客户端和服务器的教程和以下代码:
服务器:
-module(echo_server).
-export([start/0, loop/1]).
% echo_server specific code
start() ->
spawn(socket_server, start, [?MODULE, 7000, {?MODULE, loop}]).
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Message} ->
Msg = binary_to_term(Message),
case Msg of
start ->
io:format("Got start message on socket ~p.~n", [Socket]),
send_count(Socket, 10),
gen_tcp:close(Socket);
Other ->
io:format("Got message on socket ~p: ~p~n",
[Socket, Other])
end;
{error, closed} ->
io:format("Got closed message on socket ~p.~n", [Socket]),
ok;
Error ->
io:format("Got bad message: ~p on socket ~p.~n", [Error, Socket])
end.
send_count(_Socket, 0) ->
ok;
send_count(Socket, Num) ->
io:format("Sending ~p to ~p.~n", [Num, Socket]),
gen_tcp:send(Socket, term_to_binary(Num)),
send_count(Socket, Num - 1).
-module(echo_client).
-export([start/0, do_stuff/0]).
send(Socket, Msg) ->
gen_tcp:send(Socket, term_to_binary(Msg)).
start() ->
dbg:tracer(),
Pid = spawn(?MODULE, do_stuff, []),
dbg:p(Pid, r).
do_stuff() ->
case gen_tcp:connect("localhost", 7000, [binary, {packet, 0}]) of
{ok, Socket} ->
send(Socket, start),
rx_loop(Socket);
Error ->
io:format("Error connecting to server: ~p~n", [Error])
end.
rx_loop(Socket) ->
receive
{tcp, Socket, Message} ->
Msg = binary_to_term(Message),
io:format("Received message: ~p~n", [Msg]),
rx_loop(Socket)
after 5000 ->
finish_loop(Socket)
end.
finish_loop(Socket) ->
receive
{tcp, Socket, Message} ->
Msg = binary_to_term(Message),
io:format("Received message: ~p~n", [Msg]),
rx_loop(Socket);
{tcp_closed, Socket} ->
io:format("Server terminated connection.~n"),
exit(normal);
Error ->
io:format("Received bad message: ~p~n", [Error]),
rx_loop(Socket)
end.
echo_server:start()
和
echo_client:start()
从同一系统上的不同外壳,按顺序。这是我看到的:
1>echo_server:start().
<0.39.0>
Got start message on socket #Port<0.2041>.
Sending 10 to #Port<0.2041>.
Sending 9 to #Port<0.2041>.
Sending 8 to #Port<0.2041>.
Sending 7 to #Port<0.2041>.
Sending 6 to #Port<0.2041>.
Sending 5 to #Port<0.2041>.
Sending 4 to #Port<0.2041>.
Sending 3 to #Port<0.2041>.
Sending 2 to #Port<0.2041>.
Sending 1 to #Port<0.2041>.
2> echo_client:start().
{ok,[{matched,nonode@nohost,1}]}
3> (<0.41.0>) << {code_server,{module,gen_tcp}}
(<0.41.0>) << {code_server,{module,inet_tcp}}
(<0.41.0>) << {#Ref<0.0.0.74>,{ok,<0.43.0>}}
(<0.41.0>) << {#Ref<0.0.0.76>,
{ok,<<4,0,0,0,2,127,0,0,1,127,0,0,1,0,0,0,3,108,111,99,97,108,
104,111,115,116,0,105,112,54,45,108,111,99,97,108,104,
111,115,116,0,105,112,54,45,108,111,111,112,98,97,99,
107,0>>}}
(<0.41.0>) << {inet_async,#Port<0.2058>,0,ok}
(<0.41.0>) << {inet_reply,#Port<0.2058>,ok}
Received message: 10
3> (<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,10>>}
Received message: 9
3> (<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,9>>}
(<0.41.0>) << {io_reply,<0.25.0>,ok}
Received message: 8
Received message: 5
Received message: 4
Received message: 3
Received message: 2
Received message: 1
3> (<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,8,131,97,7,131,97,6>>} %% <---This guy here
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,5>>}
(<0.41.0>) << timeout
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,4>>}
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,3>>}
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,2>>}
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
(<0.41.0>) << {tcp,#Port<0.2058>,<<131,97,1>>}
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << {tcp_closed,#Port<0.2058>}
(<0.41.0>) << timeout
Server terminated connection.
3> (<0.41.0>) << timeout
(<0.41.0>) << {io_reply,<0.25.0>,ok}
(<0.41.0>) << timeout
lo
上的网络流量,我看到每个倒计时的数字都非常干净的 PSH/ACK 对。我在上面指出的行显示了两个数据包出现在一条消息中:7 和 6。它们作为两个单独的 TCP 数据包通过网络传入。任何人都知道为什么他们会被混在一起或如何解开他们?
最佳答案
为什么它们在接收端被“弄脏”:因为 TCP 是一种流协议(protocol),
并且发送/接收调用不需要与
网络数据包(即使它们通过网络以这种方式到达)。
如何“取消”它们:更改您的 TCP 协议(protocol)以包含消息分隔符,
因此您可以从流中提取消息,而无需知道数据包的位置
边界是;或使用 UDP 而不是 TCP。
关于erlang - gen_tcp smushed 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4209261/
我想实现一个 Erlang 解释器,最近我在阅读 Erlang 的标准库源代码。我在 erlang.erl 中找到 erlang:display/1 的源代码是: %% display/1 -spec
我即将用 Erlang 构建一个系统(我的问题可以用多个 CPU 更好地解决),我已经浏览了: 向你学习一些 Erlang; Erlang/OTP 在行动 编程 Erlang(阿姆斯壮); Erlan
我真的很难理解 Erlang 中的尾递归。 我有以下 eunit 测试: db_write_many_test() -> Db = db:new(), Db1 = db:write(f
我不确定或者我不知道 erlang 定位不在默认目录中的模块的传统方式。我知道它会查看已编译的文件夹和 erlang 的系统文件夹或 - lists:foreach(fun (E) -> io:fwr
对 Erlang 完全陌生。我正在尝试为函数组合定义一些函数,例如 compose , juxt和 pipe但是遇到这样一个事实,即 Erlang 没有(据我所知)可变参数,因此很难只编写适用于所有输
在 Erlang 中存储和管理高性能可变对象的最佳方法是什么?假设我想编写具有实时游戏玩法的非常简单的在线游戏服务器。不知何故,我需要在 Erlang 内存中表示玩家的状态。例如,它可能只是一个简单的
警告:前面的 erlang n00b。 我正在尝试掌握 erlang,只是尝试与牛仔一起使用基本的 hello world 应用程序。我正在模拟一个错误,基本上是在我的代码中的某处返回一个无效值,并试
当你向 shell 进程发送消息时,你可以通过调用: c:flush(). 来清除所有消息。 C:\Windows\System32>erl Eshell V5.9(使用 ^G 中止) 1> 自我()
这应该是一个简单的问题,但我不太了解文档,无法找到答案。 如果 OTP 管理器在崩溃后重新启动 gen_server,新子进程是否继承了崩溃进程的消息队列,或者消息是否在崩溃之前发送但尚未由旧子进程处
我将 net_ticktime 值设置为 600 秒。 net_kernel:set_net_ticktime(600) 在 net_ticktime = TickTime 的 Erlang 文档中:
我正在监视一个 Erlang 应用程序,我目前正在尝试确定特定 PID 已经运行了多长时间。绝对时间戳或持续时间对我有用,但我在 process_info 或通过 sys 模块看不到这些数据位中的任何
我想重新定义查找特定单词的元组的顺序 例如,我有一个像这样的元组列表: [{"a",["r001"]}, {"bi",["bidder"]}, {"bo",["an"]}] 但有时元组的顺序可能会
以下几行出现在 http://aosabook.org/en/riak.html 中,在该部分的第二段: 15.1. Erlang 简介 : "Calling the function with a
我认为 Erlang 节点之间的消息不应该很大。如果我想构建一个流服务器,通常每个连接都需要很大的带宽,Erlang 能做好吗?如果是,是否有任何开源代码可供我学习?我了解到 Erlang 很适合处理
下一个代码在结果中给了我 5.999999999999998,但正确答案是 6。 Alpha = math:acos((4*4 + 5*5 - 3*3) / (2*4*5)) Area = 1/2 *
注意:这是我的 previous question 的进化延续。关于类似的话题。 一段时间以来,我一直在寻找有关部署和更新 Erlang/OTP 版本(一组应用程序)的“最佳实践”,但我找不到任何直接
我试图在头文件中指定一个函数。 像这样: -spec update(pid(), tuple(tuple(), integer(), atom()), tuple(atom(), atom())) -
所以我在过去的八个小时里一直在使用 Erlang,我花了两个时间用我的头敲击键盘试图找出我的控制台不断返回的异常错误。 我正在编写一个骰子程序来学习erlang。我希望它能够通过 erlang 解释器
当我编译以下模块时: -module(x). -export([inp/0]). f(X) -> g(X). g(X) -> error(X). inp() -> f(123)
我目前正在开发一个实时媒体服务器,它将允许普通消费者向我们发送实时视频。在我们当前的环境中,我们已经看到发送给我们的广播持续了几天,因此能够在不断开用户连接的情况下修复错误(或添加功能)的想法非常引人
我是一名优秀的程序员,十分优秀!