gpt4 book ai didi

sockets - tcp 服务器和客户端交互

转载 作者:可可西里 更新时间:2023-11-01 02:51:11 26 4
gpt4 key购买 nike

我有一个接受连接的 tcp 服务器(echo 服务器)。我有一个客户端,当给定一个参数 N 时,它会创建许多试图与 tcp 监听器连接的进程。我能够连接到这些套接字,但我可以看到许多套接字被关闭,这很明显,因为我使用的是回显服务器,我没有收到我发送的回消息。

服务器端代码(关键部分)

 init(State = #state{port = Port}) ->
ets:new(csockets, [ordered_set, named_table, public]),
ets:insert(csockets, {sockets, []}),
case gen_tcp:listen(Port, ?TCP_OPTIONS) of
{ok, LSocket} ->
NewState = State#state{lsocket = LSocket},
spawn(fun() -> accept(NewState) end),
{ok, NewState};
{error, Reason} ->
{stop, Reason}
end.


accept(State = #state{lsocket = LSocket}) ->
case gen_tcp:accept(LSocket) of
{ok, Socket} ->
{sockets, List} = hd(ets:lookup(csockets, sockets)),
NewList = [Socket | List],
ets:insert(csockets, {sockets, NewList}),
Pid = spawn(fun() ->
loop(Socket)
end),
gen_tcp:controlling_process(Socket, Pid),
accept(State);
{error, closed} -> State
end.


loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
loop(Socket);
{error, closed} ->
io:format("CLOSED ~n"), %% This asserts for many sockets
ok
end.

连接到服务器的客户端。创建连接到服务器的 N 个进程,然后使用返回的套接字进行通信。

     send(State = #state{}) ->
Low = State#state.low,
High = State#state.high + 1,
case Low of
High -> State;
_Else ->
N = Low rem 10,
Dest = lists:nth(1, State#state.dest),
spawn(?MODULE, loop, [Dest, Low]),
NewState = State#state{low = Low + 1},
send(NewState)
end.

loop({IP, Port}, Low) ->
case gen_tcp:connect(IP, Port, [binary, {active, true}]) of
{ok, Socket} ->
io:format("~p Connected ~n", [Low]),
gen_tcp:send(Socket, "Hi"),
receive
{tcp, RecPort, Data} ->
io:format("I have received ~p on port ~p ~n", [Data, RecPort])
end;
_Else ->
io:format("The connection failed ~n")
loop({IP, Port}, Low)
end.

最佳答案

错误在你的服务器上,你正在模式匹配一​​个关闭的 tcp 上的消息:

{error, closed} 

但如果您查看 gen_tcp 的文档,您会注意到您应该对这些消息进行模式匹配:

{tcp_closed, Socket}
{tcp_error, Socket, Reason}

更新您的代码:

loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
loop(Socket);
{tcp_error, Socket} ->
io:format("CLOSED ~n"), %% This asserts for many sockets
ok
end.

你应该开始在屏幕上显示 CLOSED 字符串

关于sockets - tcp 服务器和客户端交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486228/

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