こんにちは、みんな、
Erlangは非常に新しく、C / C ++/Javaから来ています。コードで遊んでいて、1秒のガイダンスで半日節約できるところまで自分自身を絞り込んでいます。つまり、私が持っているのは、freeswitch eslポートに接続し、fs_cliの場合と同じようにポートにコマンドを発行できるようにする小さなtelnetクライアントです。(主なことは…telnet経由で通信できるはずのポートと通信しようとしていることだと思います)。Linux telnetが正常に機能しているときに、erlangアプリが失敗します。問題は単純で微妙だと確信しています。助けていただければ幸いです。
したがって、Linuxtelnetを使用してセッションを実行する方法は次のとおりです。
$>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!