使用Socket关键字实现客户端与服务器端登录验证

该示例展示了如何使用Java的Socket编程实现客户端与服务器之间的登录信息传递。客户端输入用户名和密码,通过ObjectOutputStream将LoginInfo对象发送到服务器。服务器端通过ObjectInputStream接收并验证登录信息,如果用户名为'admin'且密码为'123456',则登录成功。
摘要由CSDN通过智能技术生成

《用户类》========================================================

public class LoginInfo implements Serializable {
    private String name;
    private String password;

    public LoginInfo() {
    }

    public LoginInfo(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

《客户端》========================================================

public class Client {
    public static void main(String[] args) {
        OutputStream outputStream=null;
        ObjectOutputStream objectOutputStream=null;
        Socket socket = null;

        try {
            Scanner input =new Scanner(System.in);
            socket=new Socket("127.0.0.1",44);
            System.out.println("输入用户名");
            String name =input.next();
            System.out.println("输入密码");
            String passWard =input.next();
            LoginInfo loginInfo = new LoginInfo(name, passWard);

            outputStream=socket.getOutputStream();
            objectOutputStream=new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(loginInfo);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null){
                    outputStream.close();
                }
                if (objectOutputStream != null){
                    objectOutputStream.close();
                }
                if (socket!=null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

《服务器》==============================================================

public class Server {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        System.out.println("服务器正在启动.....");
        System.out.println("服务器端启动成功!");

        try {
            serverSocket = new ServerSocket(44);
            while (true){
                Socket socket = serverSocket.accept();//接受客户端的socket
                ServerThread serverThread=new ServerThread(socket);
                serverThread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

《多线程》=============================================================

public class ServerThread extends Thread{
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream=socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            LoginInfo loginInfo = (LoginInfo)objectInputStream.readObject();
            if (loginInfo.getName().equals("admin")&&loginInfo.getPassword().equals("123456")){
                System.out.println("登录成功!");
            }else {
                System.out.println("登录失败!");
            }
            objectInputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值