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]
[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]
- Erlang TCP编程:聊天室
- Erlang TCP编程Demo
- 网络编程TCP协议-聊天室
- erlang 聊天室
- TCP网络编程之chat聊天室
- Erlang 聊天室程序( 一)
- Erlang 聊天室程序( 一)
- Erlang 聊天室程序
- TCP聊天室
- TCP 聊天室
- TCP聊天室
- 【SOCKET编程】基于C++的TCP/UDP的聊天室
- linux网络编程练习---聊天室(TCP/IP实现)
- TCP编程——实现多人聊天室功能
- 14 QT TCP网络编程与网络聊天室的实现
- 【Erlang】基于gen_tcp的聊天室
- Qt---基于TCP聊天室
- tcp聊天室转发消息
- 调试技巧 —— 如何利用windbg + dump + map分析程序异常
- 匿名对象
- java中JFreeChart中文乱码解决方式(二)
- 网页设计
- HTML5/Javascript 2D游戏引擎列表
- Erlang TCP编程:聊天室
- 让暴风雨来的更猛烈些吧!
- java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-base
- 上周学习内容大放送(周五)
- 简介Java 标准日志工具 Log4j 的使用
- 想要过一辈子的情侣一定要看,一定要做到哦,一篇日志写出了心声…
- 业务顾问学ABAP一年多的感受
- Android Http
- 流氓软件及反流氓软件的技术分析2