实现小聊天室基础,简洁版聊天

目    录

1.聊天室项目需求,必须完成的功能点:

2.两个独立的项目如何放在同一个窗口中运行

3.服务端代码如下:

4.客户端的代码如下:


1.聊天室项目需求,必须完成的功能点:

  • 用户名登录注册(判断有没有重复用户名,可设置ip和端口)
  • 上下线提醒
  • 在线列表
  • 私聊
  • 公聊
  • 发送文字,文件。
  • 聊天记录 保存 查询 删除。
  • 下线

2.两个独立的项目如何放在同一个窗口中运行

注意:客户端和服务端是两个独立的项目,但是为了方便,可以放在同一个窗口中!然后将下列代码分包放入并且执行即可!

新建一个空项目Empty Project ---> next 然后给空项目命名 ---> finish

 

 然后弹出下面的窗口,选择Modules ---> 加号 ---> New Moudule ---> ok

 然后继续点加号,创建客户端和服务端的项目,下图一创建好,然后点击 ok

 最后整个项目的布置是这个样子的


3.服务端代码如下:

定义发送接收相关内容的ChatMsgBean类:

import java.io.Serializable;

public class ChatMsgBean implements Serializable {
    private static final long serialVersionUID = 1L;
    public String reciver;     //接收者
    public String sender;      //发送者
    public String content;     //发送内容
    public long time;          //时间
    public int msgType;        //消息标志
    public String fileName;    //文件名
    public long fileLength;    //文件长度
    public byte[] fileData;    //文件字节

    public ChatMsgBean(String reciver, String sender, String content, long time, int msgType) {
        this.reciver = reciver;
        this.sender = sender;
        this.content = content;
        this.time = time;
        this.msgType = msgType;
    }

    public ChatMsgBean(String reciver, String sender, String content, long time, int msgType, String fileName, long fileLength, byte[] fileData) {
        this.reciver = reciver;
        this.sender = sender;
        this.content = content;
        this.time = time;
        this.msgType = msgType;
        this.fileName = fileName;
        this.fileLength = fileLength;
        this.fileData = fileData;
    }
}

 输入输出流的工具类InputAndOutputUtil:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class InputAndOutputUtil {
    public InputAndOutputUtil() {
    }

    public static byte[] readFile(String path) {
        File file = new File(path);
        byte[] datas = null;
        if (!file.exists()) {
            datas = null;
        } else {
            try {
                //读文件
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(file);
                byte[] data = new byte[1048576];
                boolean var6 = false;

                int len;
                while((len = fis.read(data)) > 0) {
                    baos.write(data, 0, len);
                }

                datas = baos.toByteArray();
                baos.flush();
                baos.close();
                fis.close();
            } catch (Exception var7) {
                var7.printStackTrace();
            }
        }

        return datas;
    }

    //写文件
    public static boolean writeFile(String path, byte[] datas) {
        try {
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(datas);
            fos.flush();
            fos.close();
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }
}

键盘录入工具类InputUtil:

public class InputUtil {
    public InputUtil() {
    }

    public static int inputIntType(Scanner sc) {
        boolean var1 = false;

        while(true) {
            try {
                int choose = sc.nextInt();
                return choose;
            } catch (Exception var3) {
                sc = new Scanner(System.in);
                System.out.println("输入的类型不正确,请重新输入一个整数:");
            }
        }
    }
}

 时间提醒工具类TimeUtil:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
    public TimeUtil() {
    }

    public static String 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
简易聊天室本次实验的目的是通过以下题目掌握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
发出的红包

打赏作者

naoguaziteng

谢谢友友的打赏!一起努力吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值