线程聊天

1 篇文章 0 订阅

package com.dasenlin.chart;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChartServer {
    private List<ServerThread> clients = null;
    public static void main(String[] args) {
        System.out.println("服务器已经启动!");
        new ChartServer().startUp();
    }
    /**
     *监听客户端的请求 
     */
    private void startUp(){
        ServerSocket ss=null;
        try {
            ss = new ServerSocket(7788);
            clients=new ArrayList<ServerThread>();
            while(true){
                Socket s = ss.accept();
                Thread t = new Thread(new ServerThread(s));
                t.start();
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(null!=ss)ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //内部类
    class ServerThread implements Runnable{
            private Socket socket;
            private BufferedReader in=null;
            private PrintWriter out=null;
            private String name=null;
            public ServerThread(Socket socket) throws IOException {
                this.socket=socket;
                this.in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                this.out = new PrintWriter(socket.getOutputStream());
                this.name="["+socket.getInetAddress()+":"+socket.getPort()+"]";
                clients.add(this);
                sendMessage(name+"上线了");
            }
    
            @Override
            public void run() {
                try {
                    this.receiveClient();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            //接受消息
            public void receiveClient() throws IOException{
                String msg=null;
                while((msg=in.readLine())!=null){
                    if("quit".equals(msg)){
                        this.out.print("disconnect");
                    }
                    sendMessage(msg);
                }
            }
            //发送消息
            public void sendMessage(String msg){
                for(ServerThread st:clients){
                    st.out.println(msg);
                }
            }
            //下线
            public void stop(){
                this.sendMessage(name+"下线");
                clients.remove(this);
            }
    }
}

 


package com.dasenlin.chart;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ChartClient {
    private Socket s=null;
    private BufferedReader in=null;
    private PrintWriter out=null;
    
    private BufferedReader sbr=null;
    private boolean flag=true;
    
    
    public static void main(String[] args) {
        new ChartClient().startUp();
    }
    //主线程用于向服务端发送数据
    private void startUp(){
        try {
            s = new Socket("127.0.0.1",7788);
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream());
            sbr = new BufferedReader(new InputStreamReader(System.in));
            new Thread(new ClientThread()).start();
            sendMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(null!=s)s.close();
                if(null!=in)in.close();
                if(null!=out)out.close();
                if(null!=sbr)sbr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void sendMessage(){
        String str=null;
        try {
            while(flag){
                str=sbr.readLine();
                if(null==str){
                    this.out.print("quit");
                    break;
                };
                this.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void receiveMessage(){
        String str=null;
        try {
            str=this.in.readLine();
            if(null==str)return;
            if("disconnect".equalsIgnoreCase(str)){
                flag=false;
            }
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    class ClientThread implements Runnable{

        @Override
        public void run() {
            while(flag){
                receiveMessage();
            }
        }
    }
    
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值