package
com.softeem.java1307se.net.many2many;
import
java.io.IOException;
import
java.net.ServerSocket;
import
java.net.Socket;
import
java.util.ArrayList;
/**
* 多对多 服务器
*
*
*/
public
class
M2MServer {
private
static
ArrayList<Socket> list =
new
ArrayList<Socket>();
public
static
void
main(String[] args) {
Socket s =
null
;
String ip =
null
;
try
{
ServerSocket ss =
new
ServerSocket(
1234
);
list =
new
ArrayList<Socket>();
System.out.println(
"服务器准备就绪 ..."
);
while
(
true
){
s = ss.accept();
list.add(s);
ip = s.getInetAddress().getHostAddress();
System.err.println( ip +
" 用户上线了 , 当前在线用户为: "
+ list.size() +
"人 !"
);
M2MSend send =
new
M2MSend(s);
send.start();
}
}
catch
(IOException e) {
list.remove(s);
System.err.println(ip +
" 已下线 , 当前在线人数为: "
+ list.size() +
" 人 !"
);
}
}
public
static
ArrayList<Socket> getList() {
return
list;
}
public
static
void
setList(ArrayList<Socket> list) {
M2MServer.list = list;
}
}