【发布时间】:2015-03-05 13:25:58
【问题描述】:
我在 OpenShift 上有 Erlang 的简单服务器,它创建套接字并等待新连接。服务器的本地 ip 是 127.10.206.129 并且服务器侦听 16000 端口。
我的服务器代码:
-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.
我的客户代码:
-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).
服务器启动没有问题。我尝试在本地主机上运行客户端并出现错误(它尝试连接的时间比超时时间长:
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)
我尝试了这种愚蠢的方式(虽然127.10.206.129是一个错误的ip,因为它是服务器本地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)
gen_tcp:connect with url怎么做?
【问题讨论】:
标签: sockets tcp erlang openshift