网络编程_手写聊天室_基础简单封装版

1.关闭流的工具类

package 网络编程_手写聊天室_基础简易封装版;

public class CloseUtlis {//关闭流的工具类
	public void close(Closeable ... targets) {
		for(Closeable target:targets ) {
			if(target!=null) {
				try {
					target.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

2.客户端接收类

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;
	}
}

3.客户端发送类

package 网络编程_手写聊天室_基础简易封装版;

//打代码时的问题
//客户端和服务器都能发送所以参数该怎摸弄
//解决 只做客户端的参数
public class Sent implements Runnable{
	private Socket client;
	private BufferedReader console;
	private DataOutputStream out;
	private boolean isRunning;
	public Sent(Socket client) {
		this.client=client;
		console=new BufferedReader(new InputStreamReader(System.in));
		try {
			out =new DataOutputStream(client.getOutputStream());
		} 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();
			}
			try {
				if(str!=null)
				out.writeUTF(str);
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("输出时出错");
				release();
			}
			
		}
		release();
	}
	public  void release() {
		CloseUtlis utlis =new CloseUtlis();
		utlis.close(console,out);
		isRunning=false;
	}
	

}

4.客户端

package 网络编程_手写聊天室_基础简易封装版;

ublic class MultiClient {
	
public static void main(String[] args) throws UnknownHostException, IOException {
	 
	
	Socket	client = new Socket("localhost",8888);
	new Thread(new Sent(client)).start();
	new Thread(new Receive(client)).start();
	
}
}

服务器

package 网络编程_手写聊天室_基础简易封装版;

public class MultiServer {
	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();
			new Thread(new channel(client)).start();
		}

	}
	static class channel implements Runnable{
		
		private  Socket client;
		private DataInputStream in;
		private int port;
		private String  buf;
		channel(Socket client){
			this.client=client;
			
		}
		
		@Override
		public void run() {
			try {
				System.out.println("建立了一个连接");
				while(true) { 
					
				enter();//输入	
				sent();//输出
				
				}
			} catch (IOException e) {
				System.out.println("多线程中出错");
			}	
		}
		public void enter() throws IOException {
			//操作:输入输出操作
			in =new DataInputStream(client.getInputStream());
			buf=in.readUTF();//读取信息
		}
		public void sent() throws IOException {
			
			DataOutputStream out =new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
			out.writeUTF(buf);
			out.flush();
			
		}
	}
}

在这里插入图片描述

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

猜你喜欢

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