1

As the question states when I set up my server it can recive messages from the client when its on the same network (different computers but same wifi), yet when the client is on another network it cant reach the server and I have no idea why it cant. Can anyone help?

-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.
  • Can the remote computer access the [`epmd`](http://erlang.org/doc/man/epmd.html) port with, say, telnet? I would bet there is NAT that blocks not whitelisted ports which prevents `gen_tcp` to happily connect. – Aleksei Matiushkin Feb 07 '20 at 17:04
  • That is excatly what I though as well, however I tried going on another network and open the same ports in order to see if this was the problem yet It did not do anything :/ – Casper Akuma H. Iversen Feb 07 '20 at 17:07

1 Answers1

1

Thanks for the help on this one :)

It turned out that it was the router at the host which had the ports open, however the Source IP Address had been set to the hosts IP address which I guess somehow limits it to only calls from the network itself :)