多线程聊天室
Posted du001011
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程聊天室相关的知识,希望对你有一定的参考价值。
基于Java的多线程聊天室
客户端:
public 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<String, Socket> clientMap = new ConcurrentHashMap<>();
private static class ExecuteClientMap implements Runnable
private Socket client;
public ExecuteClientMap(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(sc.hasNextLine())
String msgFromClient = sc.nextLine();
if(msgFromClient.startsWith("R:"))
//注册
//用户名
String username = msgFromClient.split(":")[1];
userRegister(username,client);
else if(msgFromClient.startsWith("P:"))
//私聊P:用户名-信息
String userName = msgFromClient.split(":")[1].split("-")[0];
String msg = msgFromClient.split("-")[1];
sendPriUser(userName,msg);
else if(msgFromClient.startsWith("G:"))
//群聊
String groupMsg = msgFromClient.split(":")[1];
group(groupMsg);
private void sendPriUser(String userName,String msg)
Socket sc = clientMap.get(userName);
PrintStream ps = null;
try
ps = new PrintStream(sc.getOutputStream());
catch (IOException e)
e.printStackTrace();
ps.println("私聊信息为:"+msg);
ps.close();
try
sc.close();
catch (IOException e)
e.printStackTrace();
private void userRegister(String username, Socket client)
clientMap.put(username,client);
String str = username+"上线了";
group(str);
private void group(String str)
Collection<Socket> values = clientMap.values();
for(Socket sc : values)
PrintStream ps = null;
try
ps = new PrintStream(sc.getOutputStream());
catch (IOException e)
e.printStackTrace();
ps.println(str);
public static void main(String[] args) throws IOException
ServerSocket ss = new ServerSocket(8888);
ExecutorService executorService = Executors.newFixedThreadPool(20);
for(int i = 0;i<20;i++)
System.out.println("等待客户端连接");
Socket sc = ss.accept();
System.out.println("连接成功,端口号为:"+sc.getPort());
//新建一个线程处理客户端的连接
executorService.submit(new ExecuteClientMap(sc));
ss.close();
以上是关于多线程聊天室的主要内容,如果未能解决你的问题,请参考以下文章