网络编程_手写聊天室_群聊过渡板

实现功能

多好友间正常聊天,登录退出提示**
如图在这里插入图片描述

此处涉及到之前的容器
点击跳转
只写了一部分以后再更容器

关闭流工具类

package 网络编程_手写聊天室_群聊过渡板;

public class MultiClient {
	
public static void main(String[] args) throws UnknownHostException, IOException {
	 
	BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//获取用户名
	System.out.println("请输入用户名: ");
	String name=str.readLine();
	Socket	client = new Socket("localhost",8888);
	new Thread(new Sent(client,name)).start();
	new Thread(new Receive(client)).start();
	
}
}

客户端接收类

package 网络编程_手写聊天室_群聊过渡板;

public class Receive implements Runnable{

	private Socket client;
	private boolean isRunning;
	private DataInputStream in;
	public Receive(Socket client) {
		this.client=client;	
		 try {
			in=new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			System.out.println("接收时出错!");
			release();
		}
	}
	@Override
	public void run() {
		isRunning =true;
		while(isRunning) {
			String str="";
			try {
				str = in.readUTF();
			} catch (IOException e) {
				System.out.println("读取时出错");
			}
			if(str!=null) {
				System.out.println(str);
			}
		}
		this.release();
		
	}
	public  void release() {
		CloseUtlis utlis =new CloseUtlis();
		utlis.close(in);
		isRunning=false;
	}

}

客户端发送类

package 网络编程_手写聊天室_群聊过渡板;

public class Sent implements Runnable{
	private Socket client;
	private BufferedReader console;
	private DataOutputStream out;
	private boolean isRunning;
	public Sent(Socket client,String name) {
		this.client=client;
		
		console=new BufferedReader(new InputStreamReader(System.in));
		try {
			out =new DataOutputStream(client.getOutputStream());
			sent(name);//将名称发送到服务器
		
		} catch (IOException e) {
		System.out.println("发送数据时出错");
		}
	}
	@Override
	public void run() {
		isRunning =true;
		while(isRunning) {
			String str="";
			try {
				str = console.readLine();
			} catch (IOException e) {
				System.out.println("读取时出错");
				release();
			}
		
				if(str!=null) {
					sent(str);
				}	
		}
		release();
	}
	public void sent(String mess) {
		try {
			out.writeUTF(mess);
			out.flush();
		} catch (IOException e) {
			System.out.println("发送时失败!");
			release();
		}
		
	}
	public  void release() {
		CloseUtlis utlis =new CloseUtlis();
		utlis.close(console,out);
		isRunning=false;
	}
	

}

客户端

package 网络编程_手写聊天室_群聊过渡板;

public class MultiClient {
	
public static void main(String[] args) throws UnknownHostException, IOException {
	 
	BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//获取用户名
	System.out.println("请输入用户名: ");
	String name=str.readLine();
	Socket	client = new Socket("localhost",8888);
	new Thread(new Sent(client,name)).start();
	new Thread(new Receive(client)).start();
	
}
}

服务器

package 网络编程_手写聊天室_群聊过渡板;

public class MultiServer {
	private static CopyOnWriteArrayList<channel> all =new CopyOnWriteArrayList<MultiServer.channel>();//channel类型的容器
	public static void main(String[] args) throws IOException {
		System.out.println("服务器开始工作");
		ServerSocket server=new ServerSocket(8888);
		boolean isRunning =true;
		while(isRunning) {
			Socket client = server.accept();
			channel c=new channel(client);
			all.add(c);//管理每一个成员
			new Thread(c).start();
		}

	}
	static class channel implements Runnable{//与服务器连接的客户端
		private String name;
		private  Socket client;
		private DataInputStream in;
		private DataOutputStream out;
		private int port;
		private boolean iSsys;//是否为官方消息
		private boolean isRunning;
		channel(Socket client) throws IOException{
			this.client=client;
			out =new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
			in =new DataInputStream(client.getInputStream());
			String name=receive();
			this.name=name;
			sent("欢迎来到聊天室");
			sentothers(name+"加入了聊天室", true);
			
		}
		
		@Override
		public void run() {
			try {
				System.out.println("建立了一个连接");
				while(true) { 
					
				String mes=receive();//输入	
				sentothers(mes,false);//向所有用户输出
				
				}
			} catch (IOException e) {
				System.out.println("多线程中出错");
			}	
		}
		public String receive() throws IOException {
			String mes="";
			mes=in.readUTF();
			return mes;
		}
		public void sentothers(String mes,boolean iSsys) throws IOException {
			
			for(channel other:all) {
				if(other==this) {
					continue;
				}
				if(!iSsys) {
				other.sent(this.name+"对全体成员说: "+mes);
				}else {
					other.sent(mes);
				}
			}
		}
		public void sent(String mes) throws IOException {
			
		
			out.writeUTF(mes);
			out.flush();
			
		}
		public  void release() {//释放资源并退出聊天室
			isRunning=false;
			CloseUtlis utlis =new CloseUtlis();
			utlis.close(in,client);
			all.remove(this);
			try {
				sentothers(this.name+" 离开了群聊",true );
			} catch (IOException e) {
				System.out.println("释放时出错");
			}
			
		}
	}
}

效果

在这里插入图片描述

发布了39 篇原创文章 · 获赞 13 · 访问量 2087

猜你喜欢

转载自blog.csdn.net/qq_44620773/article/details/104337122