1

正如问题所述,当我设置服务器时,它可以在同一网络(不同的计算机但相同的 wifi)上接收来自客户端的消息,但是当客户端在另一个网络上时,它无法访问服务器,我不知道为什么它不能。任何人都可以帮忙吗?

-module(client).
-export([client/2, clientDNS/4]).
client(PortNo,Message) ->
IP = {1, 1, 1, 1},  % Edited for privicy
{ok,Sock} = gen_tcp:connect(IP,PortNo,[{active,false},{packet,1}]),
Data = term_to_binary(Message),
gen_tcp:send(Sock,Data),
{ok, A} = gen_tcp:recv(Sock,0),
gen_tcp:close(Sock),
Tmp = list_to_binary(A),
Res = binary_to_term(Tmp),
Res.


-module(server_http).
-export([start/1, server/2, loop/2]).

start(LPort) ->
case gen_tcp:listen(LPort,[{active, false},{packet,1}]) of
    {ok, ListenSock} ->
        start_servers(ListenSock),
        {ok, Port} = inet:port(ListenSock),
        Port;
    {error,Reason} ->
        {error,Reason}
end.

start_servers(LS) ->
Pid = setup:setupServer(),
spawn(?MODULE,server,[Pid, LS]),
ok.

server(Pid, LS) ->
case gen_tcp:accept(LS) of
    {ok,S} ->
        loop(Pid, S),
        server(Pid, LS);
    Other ->
        io:format("accept returned ~w - goodbye!~n",[Other]),
        ok
end.

loop(Pid, S) ->
inet:setopts(S,[{active,once}]),
receive
    {tcp,S,Data} ->
        Tmp = list_to_binary(Data),
        case binary_to_term(Tmp) of
            {Mod, Inputs} -> Answer = server:Mod(Pid, Inputs),
                             Res = term_to_binary(Answer),
                             gen_tcp:send(S,Res),
                             loop(Pid, S);
            {Mod}         -> Answer = server:Mod(Pid),
                             Res = term_to_binary(Answer),
                             gen_tcp:send(S,Res),
                             loop(Pid, S)
        end;
    {tcp_closed,S} ->
        io:format("Socket ~w closed [~w]~n",[S,self()]),
        ok;
    _ -> loop(Pid, S)

end.
4

1 回答 1

1

感谢您对此的帮助:)

原来是主机上的路由器打开了端口,但是源 IP 地址已设置为主机 IP 地址,我猜这以某种方式将其限制为仅来自网络本身的调用:)

于 2020-02-08T15:56:11.443 回答