gpt4 book ai didi

sockets - 为什么 socket 关闭了Erlang

转载 作者:行者123 更新时间:2023-12-03 11:57:02 24 4
gpt4 key购买 nike

我是Erlang的新手。
我的问题是,当我第一次启动客户端时,一切似乎都还好,我得到了排序后的列表:<< 1,5,72,97,108,108,111 >>。
但是到第二次它不会收到排序列表,因为我认为套接字已关闭。客户端的输出为“连接已关闭”。

这是我的代码:

客户

-module(client).

-export([client/0]).

client() ->
case gen_tcp:connect("localhost", 6000, [{mode, binary}]) of
{ok, Sock} ->
Data = [1, "Hallo", 5],
gen_tcp:send(Sock, Data),
receive
{tcp, _, Bin} ->
io:fwrite("Received sorted list from server: ~w~n", [Bin]);
{tcp_closed, _} ->
io:fwrite("Connection closed"),
gen_tcp:close(Sock)
end;
{error,_} ->
io:fwrite("Connection error! Quitting...~n")
end.

服务器
-module(server).

-export([server/0]).
-import(mergeSort,[do_recv/1]).

%creates a tcp socket on Port 6000
server() ->
{ok, Listen} = gen_tcp:listen(6000, [{keepalive, true}, %send keepalive packets
{reuseaddr, true}, %reuse address
{active, once}, %socket is active once
{mode, list}]), %binary traffic
spawn(fun() -> parallel_connection(Listen) end).

%server is listening
%accepts the connection
%starts MergeSort
parallel_connection(Listen) ->
io:fwrite("Listening connections..~n"),
{ok, Socket} = gen_tcp:accept(Listen),
io:fwrite("Connection accepted from ~w~n", [Socket]),
spawn(fun() -> parallel_connection(Listen) end),
do_recv(Socket).

合并排序
-module(mergeSort).

-export([do_recv/1]).

merge_sort(List) -> m(List, erlang:system_info(schedulers)).

%break condition
m([L],_) ->
[L];

%for more than one scheduler
m(L, N) when N > 1 ->
{L1,L2} = lists:split(length(L) div 2, L),
%self () returns Pid, make_ref() returns almost unique reference
{Parent, Ref} = {self(), make_ref()},
%starts a new process for each half of the list
%and sends Message to Parent
spawn(fun()-> Parent ! {l1, Ref, m(L1, N-2)} end),
spawn(fun()-> Parent ! {l2, Ref, m(L2, N-2)} end),
{L1R, L2R} = receive_results(Ref, undefined, undefined),
lists:merge(L1R, L2R);
m(L, _) ->
{L1,L2} = lists:split(length(L) div 2, L),
lists:merge(m(L1, 0), m(L2, 0)).

receive_results(Ref, L1, L2) ->
receive
{l1, Ref, L1R} when L2 == undefined -> receive_results(Ref, L1R, L2);
{l2, Ref, L2R} when L1 == undefined -> receive_results(Ref, L1, L2R);
{l1, Ref, L1R} -> {L1R, L2};
{l2, Ref, L2R} -> {L1, L2R}
after 5000 -> receive_results(Ref, L1, L2)
end.

do_recv(Socket) ->
%{ok, {Address, _}} = inet:peername(Socket),
receive
{tcp, Socket, List} ->
try
Data = merge_sort(List),
gen_tcp:send(Socket, Data),
io:fwrite("Sent sorted list to ~w | Job was done! Goodbye :)~n", [Socket]),
gen_tcp:close(Socket)
catch
_:_ ->
io:fwrite("Something went wrong with ~w | Worker terminated and connection closed!~n", [Socket]),
gen_tcp:close(Socket)
end;
{tcp_closed, _} ->
io:fwrite("Connection closed ~n");
{error, _} ->
io:fwrite("Connection error from ~w | Worker terminated and connection closed!~n", [Socket]),
gen_tcp:close(Socket)
end.

谁能帮我?

最佳答案

当您调用client:client/0时,它将创建一个连接,发送其数据,接收响应,然后返回。同时,服务器关闭套接字。再次调用client:client/0时,它再次创建一个连接并发送数据,但是随后它收到前一个套接字的tcp_closed消息,然后返回。

您可以通过在receive模式中指定客户端套接字来解决此问题:

        receive
{tcp, Sock, Bin} ->
io:fwrite("Received sorted list from server: ~w~n", [Bin]);
{tcp_closed, Sock} ->
io:fwrite("Connection closed"),
gen_tcp:close(Sock)
end;

在此代码中,变量 Sock替换了原始代码中 {tcp, _, Bin}{tcp_closed, _}元组中的两个下划线。这将强制消息仅针对指定的套接字匹配。

关于sockets - 为什么 socket 关闭了Erlang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48270121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com