tcp编程:聊天室
服务器
package com.sundear.demo.chat4; import com.sundear.demo.chat3.SxtUtils; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.concurrent.CopyOnWriteArrayList; /** * 在线聊天室:服务器 * 群聊 * 多线程 */ public class Chat { //高并发容器 线程安全 private static CopyOnWriteArrayList<Channel> all =new CopyOnWriteArrayList<Channel>(); public static void main(String[] args) throws IOException { System.out.println("----server----"); ServerSocket server = new ServerSocket(8888); while(true){ Socket client = server.accept(); System.out.println("一个客户建立了连接"); Channel c = new Channel(client); all.add(c);//管理所有的成员 new Thread(c).start(); } } //一个客户等于一个Channel static class Channel implements Runnable{ private DataInputStream dis; private DataOutputStream dos; private Socket client; private boolean isRunning ; private String name; public Channel(Socket client){ this.client =client; try { dis =new DataInputStream(client.getInputStream()); dos =new DataOutputStream(client.getOutputStream()); isRunning =true; //获取名称 this.name = receive(); this.send("欢迎你的到来"); sendOthers(this.name+"来了nono聊天室",true); } catch (IOException e) { release(); } } //接收消息 private String receive(){ String msg=""; try { msg = dis.readUTF(); } catch (IOException e) { System.out.println("----2---"); release(); } return msg; } //发送消息 private void send(String msg){ try { dos.writeUTF(msg); } catch (IOException e) { System.out.println("---3----"); release(); } } //群聊 private void sendOthers(String msg,Boolean isSys){ for(Channel other:all){ if(other==this){ continue; } if(!isSys) { other.send(this.name + ":" + msg);//群聊消息 }else{ other.send(msg);//系统消息 } } } //释放资源 private void release(){ this.isRunning =false; SxtUtils.close(dis,dos,client); all.remove(this); sendOthers(this.name+"离开了nono聊天室",true); } @Override public void run() { while(isRunning) { String msg=receive(); if(!msg.equals("")){ //send(msg); sendOthers(msg,false); } } } } }
发送端
package com.sundear.demo.chat4; import com.sundear.demo.chat3.SxtUtils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; /** * 发送端 */ public class Send implements Runnable{ private BufferedReader console; private DataOutputStream dos; private Socket client; private boolean isRunning; private String name; public Send(Socket client,String name){ this.client = client; this.name =name; isRunning = true; console = new BufferedReader(new InputStreamReader(System.in)); try { dos = new DataOutputStream(client.getOutputStream()); send(name); } catch (IOException e) { System.out.println("--1---"); release(); } } //释放资源 private void release(){ this.isRunning =false; SxtUtils.close(dos,client); } @Override public void run() { while(isRunning) { String msg=getStrFromConsole(); if(!msg.equals("")){ send(msg); } } } private void send(String msg){ try { dos.writeUTF(msg); dos.flush(); } catch (IOException e) { System.out.println("---3---"); release(); } } private String getStrFromConsole(){ try{ return console.readLine(); }catch (IOException e){ e.printStackTrace(); } return ""; } }
接收端
package com.sundear.demo.chat4; import com.sundear.demo.chat3.SxtUtils; import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; /** * 接收端 */ public class Receive implements Runnable{ private DataInputStream dis; private Socket client ; private Boolean isRunning; public Receive(Socket client){ this.client =client; try { dis = new DataInputStream(client.getInputStream()); isRunning =true; } catch (IOException e) { System.out.println("---1---"); release(); } } //释放资源 private void release(){ this.isRunning =false; SxtUtils.close(dis,client); } private String receive(){ String msg=""; try { msg = dis.readUTF(); } catch (IOException e) { System.out.println("----4---"); release(); } return msg; } @Override public void run() { while(isRunning){ String msg =receive(); if(!msg.equals("")){ System.out.println(msg); } } } }
客户端:实现多人聊天可以开多个客户端
package com.sundear.demo.chat4; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; /** * 在线聊天室:客户端 * 多线程实现多个客户正常接收多条信息 */ public class MultiClient { public static void main(String[] args) throws IOException { System.out.println("----client----"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入用户名"); String name =br.readLine(); Socket client = new Socket("localhost", 8888); new Thread(new Send(client,name)).start(); new Thread(new Receive(client)).start(); } }