Erlang中的tcp_socket行为不一致

时间:2017-03-13 02:54:37

标签: c sockets erlang tcpsocket

为了一次读取输入1个字符,我编写了一个使用ssty raw的c程序。

我使用这个character.c程序调用一个erlang文件naive_tcp.erl,它通过套接字将消息发送到同一台机器上的监听服务器。

我希望这个服务器naive_tcp2.erl用26行降序整数和80字符长字符串写回2" |"管道,它在80x26终端排队。

相反,会发生非常不一致的数据打印。运行./character.c,然后每隔几秒按一下键盘上的一个键,您可以看到以"26"开头并向下降到"1"的输出,但您可以看到它下降到{{ 1}},"23"或仅"21"

character.c:

"24"

naive_tcp.erl:

#include<stdio.h>
#include<stdlib.h>

int main(void){
  system ("clear");
  int c;
  /* use system call to make terminal send all keystrokes directly to stdin */
  system ("/bin/stty raw");
  while((c=getchar())!= '.') {
    char command[100];
    sprintf(command, "erl -noshell -run naive_tcp go -s erlang halt");
    system(command);
    printf("\r\n");

    /* type a period to break out of the loop, since CTRL-D won't work raw */
  }
  /* use system call to set terminal behaviour to more normal behaviour */
  system ("/bin/stty cooked");
  system ("clear");
  system ("echo ok\n");
  return 0;
}

naive_tcp2.erl

-module(naive_tcp).
-compile(export_all).

go() ->
  Text = "hello",
  {ok, Socket} = gen_tcp:connect({127,0,0,1}, 8094, []),
  send(Socket, Text).

format() ->
  format(26).
format(0) -> io:format("\r\n");
format(N) -> 
  io:format(integer_to_list(N)),
  io:format("        |                |                                                      "),
  format(N - 1).


send(Socket, Msg) ->
  inet:setopts(Socket, [{active, once}]),
  gen_tcp:send(Socket, Msg),
  receive
    {tcp, Socket, <<"quit", _/binary>>} ->
      gen_tcp:close(Socket);
    {tcp, _, Res} ->      
      gen_tcp:close(Socket),
      io:format(Res ++ "\r\n")
  end.

正在运行-module(naive_tcp2). -compile(export_all). go() -> go(8094). go(Port) -> Pid = spawn_link(fun() -> {ok, Listen} = gen_tcp:listen(Port, [binary, {active, false}]), spawn(fun() -> acceptor(Listen) end), timer:sleep(infinity) end), {ok, Pid}. acceptor(ListenSocket) -> {ok, Socket} = gen_tcp:accept(ListenSocket), spawn(fun() -> acceptor(ListenSocket) end), handle(Socket). format(Socket) -> format(26, Socket). format(0, Socket) -> gen_tcp:send(Socket, "\r\n"); format(N, Socket) -> gen_tcp:send(Socket, integer_to_list(N)), gen_tcp:send(Socket, " | | "), format(N - 1, Socket). handle(Socket) -> inet:setopts(Socket, [{active, once}]), receive {tcp, Socket, <<"quit", _/binary>>} -> gen_tcp:close(Socket); {tcp, Socket, Bin} -> % gen_tcp:send(Socket, Bin) % Msg = binary:bin_to_list(Bin), format(Socket) end, handle(Socket). 并按键:

./character

0 个答案:

没有答案