1
来自C/C++/Java的Erlang非常新颖。一直在玩代码,并让自己缩小到一秒钟的指导可能为我节省半天的时间。所以我有一个小型的telnet客户端,我打算连接到freeswitch esl端口,让我发出命令到端口,就像我在fs_cli中一样。 (我想最主要的是......我试图和一个端口通话,我应该可以通过telnet进行通信)。在Linux远程登录运行良好时,erlang应用程序失败。我相信这个问题很简单,很微妙。任何帮助感激!
所以,这里的会话如何去使用Linux的telnet:
$>telnet localhost 8021
Trying localhost...
Connected to localhost.
Escape character is '^]'.
Content-Type: auth/request
auth password<ENTER>
<ENTER>
Content-Type: command/reply
Reply-Text: +OK accepted
log 1<ENTER>
<ENTER>
Content-Type: command/reply
Reply-Text: +OK log level 1 [1]
...好吧,这是我的Telnet客户端代码:
-module(getty).
-export([s/0]).
%-define(LISTEN_PORT, 9000).
%-define(TCP_OPTS, [binary, {packet, raw}, {nodelay, true}, {reuseaddr, true}, {active, once}]).
s() ->
case gen_tcp:connect("localhost", 8021,[{active,false},{packet,2}]) of
{ok,Sock} ->
io:format("~p Connected to localhost 8021.~n", [erlang:localtime()]),
main_loop(Sock);
Error ->
io:format("Error: ~p~n", [Error])
end.
main_loop(Sock) ->
Command = get_user_input("Command> "),
spawn(fun() -> ex_cmd(Sock, Command) end),
main_loop(Sock).
ex_cmd(Sock, Command) ->
B = gen_tcp:recv(Sock, 0),
io:format("Response: ~p~n", [B]),
gen_tcp:send(Sock, Command),
A = gen_tcp:recv(Sock, 0),
%gen_tcp:close(Sock),
io:format("Response: ~p~n", [A]).
get_user_input(Prompt) ->
A1 = string:concat(
string:strip(% remove spaces from front and back
string:strip(% remove line-feed from the end
io:get_line(Prompt), right, $\n)), "\r\n\r\n"),
io:format("Command is: ~p~n", [A1]),
A1.
...这里是使用Erlang的客户端的运行:
$>erl
Erlang R15B01 (erts-5.9.1) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1 (abort with ^G)
1> c(getty).
{ok,getty}
2> getty:s().
{{2012,7,12},{10,15,0}} Connected to localhost 8021.
Command> auth password
Command is: "auth password\r\n\r\n"
Response: {error,closed}
Response: {error,closed}
Command>
使用erlang客户端的不同结果的任何线索? TIA!
是的,就是这样。谢谢! 当然,当我学习和修改代码...我知道我是Erlang世界中的业余逻辑,所以我继续改进和修改。但是,你直接面对问题时头痛目眩。去{包,原始}和其他一些选项搞砸了,事情就像我期望的那样工作。 再次感谢您花时间回答;不胜感激! – 2012-07-12 17:51:32