forked from dkgee/SimpleChatSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatServer.java
44 lines (40 loc) · 957 Bytes
/
ChatServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.tk.chat;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
/**
* 功能:聊天服务端
* 描述:
* @author jinhuatao
* @date 2015-3-5 上午11:51:57
*/
public class ChatServer {
public static void main(String[] args) {
ServerSocket server = null;
try{
//启动服务端
server = new ServerSocket(12345);
System.out.println("Server start:"
+server.getInetAddress().getHostAddress()+":"
+server.getLocalPort());
//客户端列表
Hashtable<String, ServerThread> clientlist =
new Hashtable<String, ServerThread>();
//监听客户端
while(true){
Socket socket = server.accept();
//启动客户端处理线程
new ServerThread(socket, clientlist).start();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}