[网络编程]——TCP_Socket通信_聊天室_客户端多线程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MyFuture_MyDream/article/details/51171201
public class CloseUtil {//工具类
	public static void closeAll(Closeable...io){
		for(Closeable temp:io){
			if (temp!=null) {
				try {
					temp.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
//数据的发送 线程
public class send implements Runnable{
	private BufferedReader console;//控制台输入流
	private DataOutputStream dos;//管道的输出流
	private boolean isRunning=true;//控制线程的标识
	public send() {
		console=new BufferedReader(new InputStreamReader(System.in));
	}
	public send(Socket client){
		this();
		try {
			dos=new DataOutputStream(client.getOutputStream());
		} catch (IOException e) {
			isRunning=false;
			CloseUtil.closeAll(dos,console);
		}//如果出现异常,关闭
	}
	//1、从控制台接收数据 
	public String getMsgFromConsole(){
		try {
			return console.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	// 2、发送数据
	public void send(){
		String msg=getMsgFromConsole();
		if (null!=msg&&!msg.equals("")) {
			try {
				dos.writeUTF(msg);
				dos.flush();//强制刷新
			} catch (IOException e) {
				//e.printStackTrace();
				isRunning=false;
				CloseUtil.closeAll(dos,console);
			}
		}
	}
	@Override//线程体
	public void run() {
		while(isRunning){
			send();
		}
	}
}
public class Receive implements Runnable{//接收线程
	private DataInputStream dis;//输入流
	private boolean isRunning=true;//线程标识
	public Receive() {
		
	}
	public Receive(Socket client) {
		try {
			dis=new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			//e.printStackTrace();
			isRunning=false;
			CloseUtil.closeAll(dis);
		}
	}
	public String Receive(){//接收数据
		String msg="";
		try {
			msg=dis.readUTF();
		} catch (IOException e) {
			//e.printStackTrace();
			isRunning=false;
			CloseUtil.closeAll(dis);
		}
		return msg;
	}
	@Override
	public void run() {//线程体
		while(isRunning){
			System.out.println(Receive());
		}
	}
}
/**
 * 创建客户端:发送数据+接收数据
 * 写出数据:输出流
 * 读取数据:输入流
 * 			输入流和输出流在同一个线程楼内,应该彼此独立
 * @author Administrator
 *
 */
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket client=new Socket("Hello_World", 7897);//服务器的端口和地址
		//控制台输入流
		new Thread(new send(client)).start();//一条路径
		new Thread(new Receive(client)).start();
	}
}
/**
 * 创建服务器
 * 写出数据:输出流
 * 读取数据:输入流
 * @author Administrator
 *
 */
public class Sever {
	public static void main(String[] args) throws IOException {
		ServerSocket sever=new ServerSocket(7897);
		Socket client=sever.accept();
		//写出数据
		//输入流
		DataInputStream dis=new DataInputStream(client.getInputStream());
		while(true){
			String msg=dis.readUTF();
			//System.out.println(msg);
			//输出流
			DataOutputStream dos=new DataOutputStream(client.getOutputStream());
			dos.writeUTF("服务器----->"+msg);
			dos.flush();
		}
	}
}
不足:客户端之间存在先后顺序!




猜你喜欢

转载自blog.csdn.net/MyFuture_MyDream/article/details/51171201