erlang蜗牛学习中(四)

socket,主动模式

被动模式,需要server调用recv时才读取数据。

主动模式,server使用receive来接收消息。

-module(test4).

-export([ttt/0]).

ttt()->
        {ok,Listen}=gen_tcp:listen(8888,[{packet,0},{active,true}]),
        {ok,Socket}=gen_tcp:accept(Listen),
        gen_tcp:close(Listen),
        loop(Socket).

loop(Socket)->
        receive
                {tcp,Socket,Bin} ->
                        io:format("hello tcp,~p~n",[Bin]),
                        loop(Socket);
                {tcp_closed,Socket}->
                        io:format("hello tcp_closed~n")
        end.
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048