Erlang gen_tcp connecting only works on same network

308 Views Asked by At

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.
1

There are 1 best solutions below

0
Casper Akuma H. Iversen On

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 :)