简单聊天室实现

多线程实现
Send:客户端发送消息实现类:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Send implements  Runnable {
    private Socket client;
    private BufferedReader console;
    private DataOutputStream dos;
    private boolean isRunning=true;
    public Send(Socket client,String name) {
        this.client=client;
        console=new BufferedReader(new InputStreamReader(System.in));
        try {
            dos=new DataOutputStream(client.getOutputStream());
            send(name);
        } catch (IOException e) {
            System.out.println("客户端构建错误");
            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("发送出错");
            release();
        }
    }

    private String getStrFromConsole(){

        try {
            return console.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void release(){
        this.isRunning=false;
        LsdUtils.close(client,console,dos);
    }
}

Receive:客户端接收消息实现类:


    import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class Receive implements Runnable{
    private Socket client;
    private DataInputStream dis;
    private boolean isRunning=true;
    public Receive(Socket client) {
        this.client=client;
        try {
            dis =new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            System.out.println("客户端发送侯建错误");
            release();
        }
    }

    @Override
    public void run() {
        while(isRunning){
            String msg=receive();
            if(!msg.equals("")){
                System.out.println(msg);
            }
        }
    }
    private String receive(){
        String msg="";
        try {
            msg = dis.readUTF();
        } catch (IOException e) {
            System.out.println("客户端接收报错");
            release();
        }
        return msg;
    }
    private void release(){
        this.isRunning=false;
        LsdUtils.close(client,dis);
    }
}


客户端类:Client

/**
 * 客户端:
 * 一个客户能很快手法信息
 */
public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("=====Client=====");
        Socket client=new Socket("localhost",8888);
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入用户名:");
        //客户端发送消息
        new Thread(new Send(client,br.readLine())).start();
        new Thread(new Receive(client)).start();
    }

}

聊天室服务器:Chat

/**
 * 在线聊天室 服务端
 * 问题:其他客户必须等待之前的客户推出,才能继续排队
 */
public class Chat {
    private static CopyOnWriteArrayList<Channel> all=new CopyOnWriteArrayList<>();
    public static void main(String[] args) throws IOException {
        System.out.println("=====Server=====");
        ServerSocket server=new ServerSocket(8888);
        while(true){
            Socket socket=server.accept();
            System.out.println("新来一个连接");
            Channel c=new Channel(socket);
            all.add(c);
            new Thread(c).start();
        }
    }
    //一个客户代表一个Channel
    static  class Channel  implements Runnable{
        private  DataInputStream dis;
        private  DataOutputStream dos;
        private Socket client;
        private boolean isRunning=true;
        private String name;
        public Channel(Socket client) {
            this.client=client;
            try {
                dis = new DataInputStream(client.getInputStream());
                dos=new DataOutputStream(client.getOutputStream());
                name=receive();
                this.send("欢迎你的到来");
                sendOthers(this.name+"来到了lsd的聊天室",true);
            } catch (IOException e) {
//                e.printStackTrace();
                System.out.println("构建报错");
                release();
            }
        }

        private String receive(){
            String msg="";
            try {
                msg = dis.readUTF();
            } catch (IOException e) {
                System.out.println("接收报错");
                release();
//                e.printStackTrace();
            }
            return msg;
        }
        private void sendOthers(String msg,boolean flag){
            for(Channel other:all){
                if(other==this){
                    continue;
                }
                if(!flag){
                    other.send(this.name+"对所有人说: "+msg);
                }else{
                    other.send(msg);//系统消息
                }

            }
        }
        private void send(String msg){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                System.out.println("发送出错");
                release();
//                e.printStackTrace();
            }
        }
        public void release(){
            this.isRunning=false;
            LsdUtils.close(dis,dos,client);
            all.remove(this);
            sendOthers(this.name+"离开了大家庭",true);
        }

        @Override
        public void run() {
            while(isRunning){
                String msg=receive();
                if(!msg.equals("")){
                    sendOthers(msg,false);
                }
            }
        }
    }
}

简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括:request,response,session,application等。 (1)制作简易聊天室,能够实现简单的页面聊天功能。 (2)制作网页计数器,要求相同的窗口内刷新页面访问次数并不增加,并且用图片来显数字。1、 熟悉request、response、session、application、out等内置对象; 2、 选择制作网页计数器程序需准备数字图片;1、进入jsp子目录,编写简易聊天室的JSP程序,聊天室的需要实现基本功能:输入昵称、聊天。 2.根据功能编写页面代码。二、网页计算器 利用内置对象application <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Counter.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 您是第位访问者! </body> </html> 简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括:request,response,session,application等。 (1)制作简易聊天室,能够实现简单的页面聊天功能。 (2)制作网页计数器,要求相同的窗口内刷新页面访问次数并不增加,并且用图片来显数字。1、 熟悉request、response、session、application、out等内置对象; 2、 选择制作网页计数器程序需准备数字图片;1、进入jsp子目录,编写简易聊天室的JSP程序,聊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值