[엘랑 발자국 0003] gen_tcp

9957 단어 erlang
일반적인 gen_tcp에 사용된 함수:
gen_tcp:listen(Port, Options)-> {ok, ListenSocket}|{error, Reason}//감청 포트
Options: 
{active,true}: 주동적인 플러그인을 만듭니다.데이터가 도착할 때 제어 프로세스는 {tcp, Socket, Data} 메시지를 보내고 클라이언트의 발송량을 제어할 수 없기 때문에 서버가 메시지를 처리하지 못할 위험이 있습니다.
{active,false}: 수동 플러그인 만들기;제어 프로세스는 gen_ 를 호출해야 합니다.tcp:recv(Socket, N)는 플러그인에서 온 데이터를 수신하여 클라이언트의 송신량을 제어할 수 있습니다.
{active,once}: 주동적인 플러그인을 만들지만, 다음 메시지를 받아야 합니다. 다시 활성화해야 합니다.
inet:setopts(Socket, Options)-> ok|{error,posix()}//socket의 일부 속성 설정
et:peername(Socket) -> {ok, {IP_Address, Port}| {error, Why}//되돌아오는 것은 클라이언트의 IP 주소와 포트 번호입니다. {N1, N2, N3, N4}는 IPV4, {K1, K2, K3, K4, K5, K6, K7, K8}을 대표합니다.
gen_tcp:accept(ListenSocket) -> {ok, Socket}|{error, Reason}//클라이언트로부터 socket 링크 수신
gen_tcp:connect(Address, Port, Options)-> {ok, Socket}|{error, Reason}//socket 서버 연결
gen_tcp:send(Socket, Packet) -> ok | {error, Reason}//데이터 전송
gen_tcp:close(Socket) -> ok//Socket 닫기
gen_tcp:shutdown(Socket, How) -> ok|{error, Reason}//socket 닫기
 
간편한 Tcp Socket 통신 구축
1 -module(tcp_socket1).                                                           

  2 -compile(export_all).                                                           

  3                                                                                 

  4 start_server() ->                                                               

  5     start_server(6000).                                                         

  6                                                                                 

  7 start_server(Port) ->                                                           

  8     {ok, ListenSocket} = gen_tcp:listen(Port, [binary,{packet, 0}, {active, true}]),

  9     io:format("Server is running ....."),                                       

 10     par_connect(ListenSocket).                                                  

 11                                                                                 

 12 par_connect(Listen) ->                                                          

 13     {ok, Socket} = gen_tcp:accept(Listen),                                      

 14     spawn(fun() -> par_connect(Listen) end),                                    

 15     loop(Socket).                                                               

 16                                                                                 

 17 loop(Socket) ->                                                                 

 18     receive                                                                     

 19         {tcp, Socket, Data} ->                                                  

 20             io:format("Server receive binary:~p~n", [Data]),                    

 21             RecStr = binary_to_term(Data),                                      

 22             io:format("Server receive binary_to_term:~p~n", [RecStr]),          

 23             gen_tcp:send(Socket,term_to_binary(RecStr)),                        

 24             io:format("Server send to client binary:~p~n", [term_to_binary(RecStr)]),

 25             loop(Socket);                                                       

 26         {tcp_closed, Socket} ->                                                 

 27             io:format("Client socket closed")                                   

 28     end.                                                                        

 29                                                                                 

 30 start_client() ->                                                               

 31     start_client("localhost", 6000).                                            

 32                                                                                                                                                                                    

 33 start_client(Address, Port) ->                                                   

 34     {ok, Socket} = gen_tcp:connect(Address, Port, [binary, {packet, 0}, {active, true}]),

 35     ok = gen_tcp:send(Socket, term_to_binary("Nice to meeet you!")),            

 36     receive                                                                     

 37         {tcp, Socket, Data} ->                                                  

 38             io:format("Client receive binary:~p~n", [Data]),                    

 39             RecStr = binary_to_term(Data),                                      

 40             io:format("Cleint receive binary_to_term:~p~n", [RecStr]),          

 41             gen_tcp:close(Socket)                                               

 42     end.                                                                        

 43                                          

두 개의 클라이언트를 열어 각각 서버와 클라이언트를 실행합니다.
서버 창에 표시되는 정보:
Eshell V5.10.4  (abort with ^G)

1> c(tcp_socket1).

{ok,tcp_socket1}

2> tcp_socket1:start_server().

Server is running .....Server receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Server receive binary_to_term:"Nice to meeet you!"

Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,

                               101,101,101,116,32,121,111,117,33>>

Client socket closedok

Server receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Server receive binary_to_term:"Nice to meeet you!"

Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,

                               101,101,101,116,32,121,111,117,33>>

Client socket closedServer receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Server receive binary_to_term:"Nice to meeet you!"

Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,

                               101,101,101,116,32,121,111,117,33>>

Client socket closed3> 

클라이언트 창에 표시되는 정보:
4> tcp_socket1:start_client().

Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Cleint receive binary_to_term:"Nice to meeet you!"

ok

5> tcp_socket1:start_client().

Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Cleint receive binary_to_term:"Nice to meeet you!"

ok

6> tcp_socket1:start_client().

Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101,

                        101,116,32,121,111,117,33>>

Cleint receive binary_to_term:"Nice to meeet you!"

ok

7> 

좋은 웹페이지 즐겨찾기