我尝试使用gen_tcp模块。 这是一个服务器端代码示例,我有一些困惑。
%% First, I bind server port and wait for peer connection
{ok, Sock} = gen_tcp:listen(7890, [{active, false}]),
{ok, Peer} = gen_tcp:accept(Sock),
%% Here client calls `gen_tcp:close/1` on socket and goes away.
%% After that I am tryin' send some message to client
SendResult = gen_server:send(Peer, <<"HELLO">>),
%% Now I guess that send is failed with {error, closed}, but...
ok = SendResult.
当我再次调用
gen_tcp:send/2
时,第二次调用将像预期的那样返回{error, closed}
。但是我想知道,为什么第一次调用成功了?我是否忽略了一些特定于tcp的细节?
这种奇怪的行为只适用于{active, false}连接。