2021/8/23

1.为服务器mode类添加了文件读入消息功能

注册时创建文件保存信息,登录时读取文件保存信息。

package qqserver.mode;
//qq服务器内部
import qqc.massage.Massage;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class qqserverm extends Thread{
    String[] f1=null;

    @Override
    public void run() {
        try {
            ServerSocket ss = new ServerSocket(8888);
            while (true) {

                Socket s = ss.accept();
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                Massage u1 = (Massage) ois.readObject();
                if(u1.getS().equals("1")) {//上线和在线通知
                    Massage m = new Massage();
                    System.out.println("账号为: " + u1.getMyid());
                    Mysql my = new Mysql();
                    String pass = my.inlt("1", u1.getMyid());
                    if (pass.equals(u1.getUserpassward())) {
                        if(Mythreads.getthread(u1.getMyid())!=null){
                            m.setS("2");
                            m.setXiaoxi("账号已在线");
                            oos.writeObject(m);
                            System.out.println("线程已存在");
                            s.close();
                        }
                        else { m.setS("1");
                            m.setMyname(my.inlt("5",u1.getMyid()));
                            m.setXiaoxi(my.inlt("2", u1.getMyid()));
                            m.setQun(my.inlt("3", u1.getMyid()));
                            ArrayList<String> zaix = new ArrayList<String>();
                            Massage ms = new Massage();
                            ms.setMyid(u1.getMyid());
                            ms.setS("11");
                            m.ss=new ArrayList<String>();
                            m.ss1=new ArrayList<Integer>();
                            m.ss2=new ArrayList<byte[]>();
                            m.qunname=new ArrayList<String>();
                            m.friendname=new ArrayList<String>();
                            if(m.getXiaoxi()!=null){
                                f1 = m.getXiaoxi().split("\\|");
                                for (int i = 0; i < f1.length; i++) {
                                    if (Mythreads.hm.containsKey(f1[i])) {
                                        zaix.add(f1[i]);
                                        ObjectOutputStream oos2 = new ObjectOutputStream(Mythreads.getthread(f1[i]).s.getOutputStream());
                                        ms.setOid(f1[i]);
                                        oos2.writeObject(ms);
                                    }
                                    m.friendname.add(my.inlt("5",f1[i]));
                                    File fl=new File("src\\qqserver\\view\\"+f1[i]+".jpg");
                                    FileInputStream fis=new FileInputStream(fl.getCanonicalPath());
                                    byte[] buf=new byte[1024];
                                    int len;
                                    while((len=fis.read(buf))!=-1){
                                        m.ss.add(f1[i]);
                                        m.ss1.add(len);
                                        m.ss2.add(buf);
                                        buf=new byte[1024];
                                    }
                                }}
                            if(m.getQun()!=null) {
                                f1 = m.getQun().split("\\|");
                                for (int i = 0; i < f1.length; i++) {
                                    m.qunname.add(my.qun("2", f1[i]));
                                    File fl = new File("src\\qqserver\\view\\" + f1[i] + ".jpg");
                                    FileInputStream fis = new FileInputStream(fl.getCanonicalPath());
                                    byte[] buf = new byte[1024];
                                    int len;
                                    while ((len = fis.read(buf)) != -1) {
                                        m.ss.add(f1[i]);
                                        m.ss1.add(len);
                                        m.ss2.add(buf);
                                        buf = new byte[1024];
                                    }
                                }
                            }
                            File fl=new File("src\\qqserver\\massage\\"+u1.getMyid()+".txt");//此为登录时读取文件信息。
                            FileInputStream fis=new FileInputStream(fl.getCanonicalPath());
                            ObjectInputStream oiss=new ObjectInputStream(fis);
                            ArrayList<Massage> mss=(ArrayList<Massage>)oiss.readObject();
                            MassageDemo.addtext(u1.getMyid(),mss);
                            oiss.close();
                            fis.close();
                            m.setZaix(zaix);
                            oos.writeObject(m);
                            System.out.println("密码正确");
                            Mythread m1 = new Mythread(s);
                            Mythreads.add(u1.getMyid(), m1);
                            m1.start();
                            System.out.println(u1.getMyid() + " " + m1 + ":加入线程");}
                    }else{
                        m.setS("2");
                        m.setXiaoxi("密码错误");
                        oos.writeObject(m);
                        System.out.println("密码错误");
                        s.close();
                    }
                }
                else if(u1.getS().equals("4")){//注册
                    Mysql my=new Mysql();
                    Massage m=new Massage();
                    if(my.selectid(u1.getMyid())){
                        m.setS("2");
                        m.setXiaoxi("账号已存在");
                    }else{

                        boolean bl=my.addid(u1.getMyid(),u1.getUserpassward(),u1.getMyname());
                        if(bl==true){
                            String s3=null;
                            FileOutputStream fos=null;
                            File fl=null;
                            for(int i=0;i<u1.ss.size();i++)
                            {

                                if(s3!=u1.ss.get(i)){
                                    s3=u1.ss.get(i);
                                    fl=new File("src\\qqserver\\view\\"+s3+".jpg");
                                    fos=new FileOutputStream(fl.getCanonicalPath());
                                }
                                fos.write(u1.ss2.get(i),0,u1.ss1.get(i));
                            }
                            File file=new File("src\\qqserver\\massage\\"+u1.getMyid()+".txt");//此为注册时创建的文件信息数据
                            FileOutputStream ffos=new FileOutputStream(file.getCanonicalPath());
                            ObjectOutputStream ooss=new ObjectOutputStream(ffos);
                            ArrayList<Massage> ma=new ArrayList<Massage>();
                            ooss.writeObject(ma);
                            ooss.flush();
                            ooss.close();
                            m.setS("4");
                            m.setXiaoxi("创建成功");
                        }
                        else{
                            m.setS("2");
                            m.setXiaoxi("创建失败");
                        }
                    }
                    oos.writeObject(m);
                }

            }

        } catch(IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public qqserverm(){

    }

    public static void main(String argv[]){
        new qqserverm().start();
    }

}

2.上线发送信息类

上线后将离线的信息和历史信息合并发送给客服端。退出时保存文件到本地。

package qqserver.mode;
//保存qq客户端离线文件
import qqc.massage.Massage;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class MassageDemo {
    public static HashMap<String, ArrayList<Massage>> hm=new HashMap<>();
    public static void add(String id,ArrayList<Massage> m){
        hm.put(id,m);
    }
    public static ArrayList<Massage> getMassage(String id){
        return (ArrayList<Massage>) hm.get(id);
    }
    public static void Massagecate(String id){
        ArrayList<Massage> ms=MassageDemo.gettext(id);//发送历史信息
        ObjectOutputStream oos=null;
        for(int i=0;i<ms.size();i++){
            try {
                oos = new ObjectOutputStream(Mythreads.getthread(id).s.getOutputStream());
                oos.writeObject(ms.get(i));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         ms=MassageDemo.getMassage(id);
        System.out.println(ms);
        if(ms!=null){//发送离线消息
        for(Massage m:ms){
            try {
                oos = new ObjectOutputStream(Mythreads.getthread(id).s.getOutputStream());
                oos.writeObject(m);
                MassageDemo.gettext(id).add(m);//离线信息保存到历史信息。
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        hm.remove(id);
        }
    }
    public static HashMap<String,ArrayList<Massage>> text=new HashMap<>();
    public static void addtext(String id,ArrayList<Massage> m){
        text.put(id,m);
    }
    public static ArrayList<Massage> gettext(String id){
        return (ArrayList<Massage>) text.get(id);
    }
    public static void Massagesave() {//此方法为服务器退出时保存所有文件到本地
        Set<String> ss=hm.keySet();
        for(String id:ss)
        {
            ArrayList<Massage> mms=null;
            if(MassageDemo.text.containsKey(id)==false){
                File fl=new File("src\\qqserver\\massage\\"+id+".txt");
                FileInputStream fis= null;
                try {
                    fis = new FileInputStream(fl.getCanonicalPath());
                    ObjectInputStream oiss=new ObjectInputStream(fis);
                    mms=(ArrayList<Massage>)oiss.readObject();
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }

            } else {
                    mms=MassageDemo.gettext(id);
            }
            ArrayList<Massage> ms=MassageDemo.getMassage(id);
            for(int i = 0; i<ms.size(); i++){
                mms.add(ms.get(i));
            }
            File file=new File("src\\qqserver\\massage\\"+id+".txt");
            FileOutputStream ffos= null;
            try {
                ffos = new FileOutputStream(file.getCanonicalPath());
                ObjectOutputStream ooss=new ObjectOutputStream(ffos);
                ooss.writeObject(mms);
                ooss.flush();
                ooss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            text.remove(id);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值