Erlang TCP编程:聊天室

来源:互联网 发布:wince导航软件下载 编辑:程序博客网 时间:2024/05/16 18:34
用Erlang写了一个聊天室的服务器Demo,感觉代码很是简洁。我用JAVA模拟了100个客户端,但是当我模拟5000个客户端时,就会得到链接异常:java.net.ConnectException: Connection refused: connect。
[b]服务器端代码:[/b]
[code="erlang"]
-module(chat_server).
-export([start_server/0, accept/1]).

start_server()->
    {ok, SocketListen} = gen_tcp:listen(9999, [binary, {active, false}]),
    ets:new(chatETS, [named_table, public]),
    ets:insert(chatETS, {socketList, []}),
    accept(SocketListen).

accept(SocketListen)->
    {ok, Socket} = gen_tcp:accept(SocketListen),
    [{socketList,SocketListETS}] = ets:lookup(chatETS, socketList),
    ets:insert(chatETS, {socketList, [Socket|SocketListETS]}),
    spawn(?MODULE, accept, [SocketListen]),
    process_req(Socket).

process_req(Socket)->
    case gen_tcp:recv(Socket, 0) of
        {ok, Message} ->
                        io:format("Received message: ~p~n", [Message]),
                        [{socketList,SocketListETS}] = ets:lookup(chatETS, socketList),
                        [gen_tcp:send(Socket, Message) || Socket <- SocketListETS],
                        process_req(Socket);
        {error, closed} ->
                        io:format("Received message: closed.~n"),
                        [{socketList,SocketListETS}] = ets:lookup(chatETS, socketList),
                        SocketListETSNew = lists:delete(Socket, SocketListETS),
                        ets:insert(chatETS, {socketList, SocketListETSNew});
        _Error->
                        io:format("Unknow error.~n")
    end.


[/code]

[b]客户端代码:[/b]
[code="java"]
import java.io.IOException;
import java.net.Socket;

public class Client{

    public static void main(String args[]) throws Exception{
        for(int i = 0; i <= 1000; i ++){
            new Thread(new ChatClient(i)).start();
            Thread.currentThread().sleep(200);
        }
    }
    
    static class ChatClient implements Runnable {
        private String threadId;
        
        public ChatClient(int chreadId){
            this.threadId = "Thread id:" + chreadId;
        }
        
        public void run(){
            Socket socket = null;
            try {
                socket = new Socket("127.0.0.1", 9999);
                int i = 0;
                while(true){
                    socket.getOutputStream().write((threadId + ", message:" + i + "\t").getBytes());
                    byte[] b = new byte[1024 * 4];
                    int length = socket.getInputStream().read(b);
                    //System.out.println(new String(b,0, length));
                    i ++;
                    Thread.currentThread().sleep(10000);
                    if(i >= 10000){
                        break;
                    }
                }
                socket.close();
            } catch (Exception e){
                e.printStackTrace();
                if(socket != null){
                    try {
                        socket.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        
    }
}
[/code]


原创粉丝点击