首页 > 其他分享 >题目:模拟网站的登录,客户端录入账号密码,然后服务器端进行验证(TCP)(完善)

题目:模拟网站的登录,客户端录入账号密码,然后服务器端进行验证(TCP)(完善)

时间:2022-09-20 21:35:55浏览次数:88  
标签:String TCP try IOException catch null public 账号密码 服务器端

完善(加入完整的处理异常的方式、多线程接收用户请求)(TCP)

封装的类

package com.gao.Project.Pro5;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = -2201118496650087459L;
    //快捷键Alt+Enter---->选第一个
    //如果没有出现,需设置
    //File-->Settings--->Inspections--->Serializable class without 'serialVersionUID'--->勾选--->OK
    private String name;
    private String pwd;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public User(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }
}

线程

package com.gao.Project.Pro5;

import java.io.*;
import java.net.Socket;

public class ServerTread extends Thread {//
    InputStream is = null;
    ObjectInputStream ois = null;
    OutputStream os = null;
    DataOutputStream dos = null;
    Socket s = null;
    public ServerTread(Socket s){
        this.s=s;

    }
    @Override
    public void run() {
        try {

            is = s.getInputStream();
            ois = new ObjectInputStream(is);

            User user = (User)(ois.readObject());

            boolean flag = false;
            if(user.getName().equals("甜甜")&&user.getPwd().equals("12323")){
                flag = true;
            }

            //向客户端输出一句话:---->操作流---->输出流
            os = s.getOutputStream();
            dos =new DataOutputStream(os);
            dos.writeUTF("你好,我是服务器端,我接收到你的请求了");

        }catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(dos!=null){
                    dos.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

客户端

package com.gao.Project.Pro5;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TestClient {//客户端
    public static void main(String[] args){
        Socket s = null;
        OutputStream os  = null;
        InputStream is= null;
        ObjectOutputStream oos = null ;
        DataInputStream dis = null;
        try {
            //录入用户的账号密码:
            s = new Socket("192.168.2.142",8888);
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的账号:");
            String name = sc.next();
            System.out.println("请输入您的密码:");
            String pwd = sc.next();
            //将账号和密码封装成User的对象:
            User user = new User(name,pwd);

            //2.对于程序员来说,向外发送数据  感受--->利用输出流
            os = s.getOutputStream();
            oos = new ObjectOutputStream(os);
            oos.writeObject(user);

            //接收服务器端的回话--->利用输入流:
            is = s.getInputStream();
            dis = new DataInputStream(is);
            boolean b = dis.readBoolean();
            if(b){
                System.out.println("登录成功");
            }else {
                System.out.println("登录失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //3.关闭流  +   关闭网络资源:
            try {
                if(dis!=null){
                    dis.close();
                }
            } catch (IOException e) {
               e.printStackTrace();
            }
            try {
                if(is!=null){
                    is.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(oos!=null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(s!=null){
                    s.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

服务器端

package com.gao.Project.Pro5;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {//服务器

    public static void main(String[] args){
        System.out.println("服务器已启动");
        ServerSocket ss = null;
        Socket s = null;
        int count = 0;//定义一个计算器,用来记录客户端的请求
        try {
            ss = new ServerSocket(8888);
            while (true){//加入死循环,服务器一直监听客户端是否发送数据
                s = ss.accept();
                //每次过来的客户端的请求  靠  线程处理:
                new ServerTread(s).start();
                count++;
                System.out.println("当前是第"+count+"个用户访问服务器,访问的用户是:"+s.getInetAddress());
            }
        } catch (IOException e) {
          e.printStackTrace();
        }

    }
}

标签:String,TCP,try,IOException,catch,null,public,账号密码,服务器端
From: https://www.cnblogs.com/gaoxiaocuo/p/16712629.html

相关文章