OOP思想封装Java在线聊天室

在线聊天室:服务端 
封装使用多线程实现多个客户可以正常收发多条信息 

/**
 * 在线聊天室:服务端 
 * 封装使用多线程实现多个客户可以正常收发多条信息 
 *
 */
public class TMultiChat{
    public static void main(String[] args) throws Exception {
        System.out.println("-----Server-----");
		// 1、指定端口使用ServerSocket
		ServerSocket socket = new ServerSocket(8888);
        while (true) {
			// 2、阻塞式等待连接accept
			Socket server= socket.accept();
			System.out.println("一个客户端建立了连接");
        }
    }

    // 一个客户代表一个Channel
	static class Channel implements Runnable{
        private DataInputStream dis;
		private DataOutputStream dos;
		private Socket server;
		private boolean isRunning;
        
        public Channel(Socket server){
            this.server = server;
            try{
                dis = new DataInputStream(server.getInputStream());
                dos = new DataOutputStream(server.getOutputStream());
                isRunning = true;
            }catch(IOException e){
                System.out.println("---1---");
                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);
                dos.flush();
            }catch(IOException e) {
                System.out.println("---3---");
				release();
            }
        }
        
        // 释放资源
		private void release(){
			this.isRunning = false;
			Utils.close(dis,dos,client);
		}

        @Override
		public void run() {
            while(isRunning){
                String msg = receive();
                if(!msg = ""){
                    send(msg);
                }
            }
        }
    }
}

在线聊天室:客户端使用多线程封装了发送端
1、发送消息
2、释放资源
3、从控制台获取消息
4、重写run

/**
 * 使用多线程封装了发送端
 * 1、发送消息
 * 2、释放资源
 * 3、从控制台获取消息
 * 4、重写run
 * @author fujun
 *
 */
public class Send implements Runnable{
    private BufferedReader console; 
	private DataOutputStream dos;
	private Socket client;
	private boolean isRunning;
	public Send(Socket client){
        this.client = clietn;
        console = new BufferedReader(new InputStreamReader(System.in));
        this.isRunning = true;
        try{
            dos = new DataOutputStream(client.getOutputStream());
        } catch(IOException e){
            System.out.println("===1===");
			this.release();
        }
    }
    
    @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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
    
    // 释放资源
	private void release(){
		this.isRunning = false;
		Utils.close(dos,client);
	}
}

在线聊天室:客户端使用多线程封装了接收端
1、接收消息
2、释放资源
3、重写run

/**
 * 使用多线程封装了接收端
 * 1、接收消息
 * 2、释放资源
 * 3、重写run
 * @author fujun
 *
 */
public class Receive implements Runnable {
	private DataInputStream dis;
	private Socket client;
	private boolean isRunning;
	public Receive(Socket client) {
		this.client = client;
		this.isRunning = true;
		try {
			dis = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			System.out.println("===2===");
			release();
		}
	}

	// 接收消息
	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);
			}
		}
	}

	// 释放资源
	private void release() {
		this.isRunning = false;
		Utils.close(dis, client);
	}

}

 

在线聊天室:客户端
封装实现多个客户可以正常收发多条信息

/**
 * 在线聊天室:客户端
 * 封装实现多个客户可以正常收发多条信息
 * @author fujun
 *
 */
public class TMultiClient {

	public static void main(String[] args) throws Exception {
		System.out.println("-----Clinet-----");
		// 1、建立连接:使用Socket创建客户端 + 服务端的地址和端口
		Socket client = new Socket("localhost",8888);
		// 2、客户端发送消息
		new Thread(new Send(client)).start();
		new Thread(new Receive(client)).start();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值