`
coderplay
  • 浏览: 571704 次
  • 性别: Icon_minigender_1
  • 来自: 广州杭州
社区版块
存档分类
最新评论

echo_server

阅读更多
代码
 
  1. -module(echo_server).  
  2. -export([start/0,stop/0]).  
  3.   
  4. -define(LISTEN_PORT,12345).     % 开放端口  
  5. -define(MAX_CONN, 5000).        % 最大连接数  
  6.   
  7. start() ->  
  8.     process_flag(trap_exit, true), % 设置退出陷阱  
  9.     tcp_server:start_raw_server(?LISTEN_PORT,  
  10.                 fun(Socket) -> socket_handler(Socket,self()) end,  
  11.                 ?MAX_CONN,   
  12.                 0).  
  13.   
  14. %% 处理数据  
  15. socket_handler(Socket,Controller) ->  
  16.     receive  
  17.         {tcp, Socket, Bin} ->  
  18.             gen_tcp:send(Socket, Bin); % echo  
  19.         {tcp_closed, Socket} ->  
  20.             ok;  
  21.         _ ->  
  22.             socket_handler(Socket,Controller)  
  23.     end.  
  24.   
  25. stop() ->  
  26.     tcp_server:stop(?LISTEN_PORT).  

基于Joe Armstrong 的tcp_server模块来做的, 试试先 :)

编译
    erl -noshell -s make all -s init stop
运行
    erl -noshell -sname coderplay -s echo_server start
分享到:
评论
3 楼 wenew 2008-03-19  
Distributed Erlang has all or nothing security [1] (so this is just too dangerous)
:erlang 用behavior来做,就没有这个问题,运行erlang的服务器不要使用root用户!
Distributed Erlang was not designed for thousands of clients
:我在http://wiki.trapexit.org/一个Non-Blocking Tcp Server上进行了简单的
stress测试,可以轻松建立15k的connections,据说可以建多达80K的connections
2 楼 wenew 2008-01-24  
好像你的不是一个echo server也,它只会接收,没有echo
1 楼 inshua 2008-01-04  
我下载了这个模块。在文档中发现一段危险的文字:

This solution does not involve distributed Erlang. Their are several reasons for this:

Distributed Erlang has all or nothing security [1] (so this is just too dangerous)

Distributed Erlang was not designed for thousands of clients


第二点很致命。博主如何看。

相关推荐

Global site tag (gtag.js) - Google Analytics