package tfthread;
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import ServerSocket;
import Socket;
import javautilArrayList;
import javautilList;
import ncurrentExecutorService;
import ncurrentExecutors;
public class ChatServer {
private static final int PORT = ; // 端口号
private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象
private ExecutorService exec;
private ServerSocket server;
public static void main(String[] args) {
new ChatServer();
}
public ChatServer() {
try {
server = new ServerSocket(PORT);
exec = ExecutorsnewCachedThreadPool();
Systemoutprintln(服务器已启动!);
Socket client = null;
while (true) {
client = serveraccept(); // 接收客户连接
listadd(client);
execexecute(new ChatTask(client));
}
} catch (IOException e) {
eprintStackTrace();
}
}
static class ChatTask implements Runnable {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private String msg;
public ChatTask(Socket socket) throws IOException {
thissocket = socket;
br = new BufferedReader(new InputStreamReader(socket
getInputStream()));
msg = 【 + thissocketgetInetAddress() + 】进入聊天室!当前聊天室有【
+ listsize() + 】人;
sendMessage();
}
public void run() {
try {
while ((msg = brreadLine()) != null) {
if (msgtrim()equals(bye)) {
listremove(socket);
brclose();
pwclose();
msg = 【 + socketgetInetAddress() + 】离开聊天室!当前聊天室有【
+ listsize() + 】人;
socketclose();
sendMessage();
break;
} else {
msg = 【 + socketgetInetAddress() + 】说 + msg;
sendMessage();
}
}
} catch (IOException e) {
eprintStackTrace();
}
}
/**
* 群发消息给聊天室的所有人
*/
private void sendMessage() throws IOException {
Systemoutprintln(msg);
for (Socket client : list) {
pw = new PrintWriter(clientgetOutputStream() true);
pwprintln(msg);
}
}
}
}
package tfthread;
import javaioBufferedReader;
import javaioInputStreamReader;
import javaioPrintWriter;
import Socket;
import ncurrentExecutorService;
import ncurrentExecutors;
public class ChatClient {
private static final int PORT = ;
private static ExecutorService exec = ExecutorsnewCachedThreadPool();
public static void main(String[] args) throws Exception {
new ChatClient();
}
public ChatClient() {
try {
Socket socket = new Socket( PORT);
execexecute(new Sender(socket));
Systemout
println(【 + socketgetInetAddress() + 】您好欢迎来到阿飞聊天室!);
BufferedReader br = new BufferedReader(new InputStreamReader(socket
getInputStream()));
String msg;
while ((msg = brreadLine()) != null) {
Systemoutprintln(msg);
}
} catch (Exception e) {
}
}
/**
* 客户端线程获取控制台输入消息
*/
static class Sender implements Runnable {
private Socket socket;
public Sender(Socket socket) {
thissocket = socket;
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
Systemin));
PrintWriter pw = new PrintWriter(socketgetOutputStream() true);
String msg;
while (true) {
msg = brreadLine();
pwprintln(msg);
if (msgtrim()equals(bye)) {
pwclose();
brclose();
execshutdownNow();
break;
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
}