Nikolay Bildeyko
Nikolay Bildeyko

Reputation: 109

How to connect to socket by TCP

I have simple server on OpenShift by Erlang, which creates socket and wait new connections. Local ip for server is 127.10.206.129 and server listens 16000 port. Code of my server:

-module(chat).
-export ([start/0, wait_connect/2, handle/2]).

start() ->
{ok, ListenSocket} = gen_tcp:listen(16000, [binary, {ip,{127,10,206,129}}]),
wait_connect(ListenSocket, 0).

wait_connect(ListenSocket, Count) ->
io:format("~s~n", ["Wait..."]),
gen_tcp:accept(ListenSocket),
receive
    {tcp, _Socket, Packet} ->
        handle(Packet, Count),
        spawn(?MODULE, wait_connect, [ListenSocket, Count+1]);
    {tcp_error, _Socket, Reason} ->
        {error, Reason}
end.

Code of my client:

-module(client).
-export ([client/2]).

client(Host, Data) ->
{ok, Socket} = gen_tcp:connect(Host, 16000, [binary]),
send(Socket, Data),
ok = gen_tcp:close(Socket).

send(Socket, <<Data/binary>>) ->
gen_tcp:send(Socket, Data).

Server starts without troubles. I tried run client on my localhost and had error (it tried to connect for time much than timeout:

 2> client:client("chat-bild.rhcloud.com", <<"Hello world!">>).
 ** exception error: no match of right hand side value {error,etimedout}
 in function  client:client/2 (client.erl, line 4)

I tried this stupid way (although 127.10.206.129 is a incorrect ip, because it's server local ip):

 3> client:client({127,10,206,129}, <<"Hello world!">>).
 ** exception error: no match of right hand side value {error,econnrefused}
 in function  client:client/2 (client.erl, line 16)

How to do gen_tcp:connect with url?

Upvotes: 0

Views: 354

Answers (2)

user2879327
user2879327

Reputation:

Only ports 80, 443, 8000, and 8443 are open to the outside world on openshift. You can not connect to other ports on OpenShift from your local workstation, unless you use rhc port-forward command, and that will only work with published ports that are used by cartridges.

Upvotes: 2

Steve Vinoski
Steve Vinoski

Reputation: 20024

Your listen call is limiting the listen interface to 127.10.206.129, but judging from the second client example, your client can't connect to that address. Try eliminating the {ip,{127,10,206,129}} option from your listen call so that your server listens on all available interfaces, or figure out what interface the server name "chat-bild.rhcloud.com" corresponds to and listen only on that interface.

Upvotes: 1

Related Questions