多线程聊天室
2020-12-13 04:31
标签:tac tput 用户 map shm user cli 用户名 close 基于Java的多线程聊天室 客户端: 服务端: 多线程聊天室 标签:tac tput 用户 map shm user cli 用户名 close 原文地址:https://www.cnblogs.com/du001011/p/11111783.htmlpublic class MultiThreadClient {
private static class RecMsg implements Runnable{
private Socket client;
public RecMsg(Socket client) {
this.client = client;
}
@Override
public void run() {
Scanner sc = null;
try {
sc = new Scanner(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(client.isClosed()){
System.out.println("客户端退出");
break;
}
if(sc.hasNextLine()) {
System.out.println("服务端说:" + sc.nextLine());
}
}
sc.close();
}
}
private static class SendMsg implements Runnable{
private Socket client;
public SendMsg(Socket client) {
this.client = client;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
PrintStream ps = null;
try {
ps = new PrintStream(client.getOutputStream(),true,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
while(true){
System.out.println("请输入内容");
String str = sc.nextLine();
ps.println(str);
if(str.contains("bye")){
break;
}
}
ps.close();
sc.close();
}
}
public static void main(String[] args) throws IOException {
Socket sc = new Socket("127.0.0.1",8888);
new Thread(new RecMsg(sc)).start();
new Thread(new SendMsg(sc)).start();
}
}
public class MultiThreadServer {
//保存所有连接的客户端
private static Map