4.16(周总结)

这周写到了客户端和服务端连接这里,已经在这里困了好几天了,主要是序列化和反序列化那里有点搞不懂,然后传递封装类的那里就很麻烦,项目的推进就极其困难。

序列化和反序列化

首先从自我来讲,这里我真的学得很不好,再加上我又不细心,所以这里真的困扰了我好几天了,但是今天终于解决了,就从这次的项目出发,其中一个很重要的点就是c/s模式,当我客户端要给服务端传东西的时候,我不可能每次都只传一个,例如登录,就要传密码和账号,所以这个时候可以把它封装成一个内部类,然后在客户端序列化,作为二进制传给服务端,然后在服务端反序列化,就可以得到用户在客户端想要传给服务端的数据。

服务端

package com.example.untitled4444;

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

public class MyServer {
    /**
     * 服务器
     */
    public static void main(String[] args) throws IOException {
        //启动服务器
        ServerSocket service = new ServerSocket(31814);
        System.out.println("服务器已启动,等待连接...");
        //等待客户端连接
        while(true) {
            //主线程接收客户端请求
            Socket socket = service.accept();
            //开启子线程去接收、发送数据
            new Thread() {
                @Override
                public void run() {
                    try {
                        new Serve().service(socket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }

    }
    static  class Serve{
        public void service(Socket socket) throws IOException {
            System.out.println("客户端已连接");
            //获取输入流
            InputStream is = socket.getInputStream();
            //字节输入流转字符流
            InputStreamReader isr = new InputStreamReader(is);
            //字符流 转 逐行读取流
            BufferedReader br = new BufferedReader(isr);

            //获取输出流
            OutputStream os = socket.getOutputStream();
            PrintStream ps = new PrintStream(os);
            Data d = new Data();
            String name = null;
            String pass = null;
            String bj=null;
            //循环接收,直到接收到0时中断
            while (true) {

                bj=br.readLine();
                d.setBj(bj);
                name = br.readLine();//接收用户名
                d.setUserName(name);
                pass = br.readLine();//接收密码
                d.setUserPass(pass);
                System.out.println(new Date());
                System.out.println("用户:" + name + ",密码:" + pass + "访问");

               if (d.getUserName().equals("ghy") && d.getUserPass().equals("123456") &&d.getBj().equals("email")) {
                    ps.println("用户身份:登录成功!");
                } else {
                    ps.println("登录失败:用户名或密码输入不正确!");
                }
            }
        }
    }
    //封装数据
    static class Data{
        private  String bj;
        private String userName;
        private String userPass;

        private String email;

        public String getBj(){
            return bj;
        }
        public void setBj(String bj){
            this.bj=bj;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getUserPass() {
            return userPass;
        }

        public void setUserPass(String userPass) {
            this.userPass = userPass;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email= email;
        }
    }

}

客户端

package com.example.untitled444;

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

public class MyClient {
    /**
     * 搭建客户端
     */
    public static Login login = new Login();
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",31814);
        //获取输出
        OutputStream os = socket.getOutputStream();
        PrintStream ps = new PrintStream(os);
        //获取输入
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        //输入登录信息
        while (true) {
            login.setBj("email");
            ps.println(login.getBj());
            login.setName("ghy");
            ps.println(login.getName());//发送用户名
            login.setPass("123456");
            ps.println(login.getPass());//发送密码
            String text = br.readLine();//接收回复
            System.out.println(text);
            break;
        }

    }

    //封装数据
    static class Login{
        private String bj;
        private String name;
        private String pass;

        private String email;

        public String getBj(){
            return bj;
        }
        public void setBj(String bj){
            this.bj=bj;
        }

        public String getName() {
            return name;
        }

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

        public String getPass() {
            return pass;
        }

        public void setPass(String pass) {
            this.pass = pass;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email =email;
        }
    }
}

这里的坑真的很多,至少我踩了两天才踩完,一直踩一直站,就是我这几天写项目的心得。

其实可以说是针对于这一点的思路,先连接,但是有很多操作,那就定义一个判断的值,例如“email”,当我的服务端从客户端得到"email"的时候,我就去读相对应的邮箱号,至于账号密码那些就不读,然后返回的时候就返回一个验证码,然后客户端会得到服务端返回的验证码,再拿从服务端得到的验证码和从界面上输入文本框的验证码做比较,一样就继续,不一样就再来。

那对于登录的思路也就随之而出了,我只要把那个判断的值改为“login”,我的服务端就能知道我是在登录,所以读的时候就读账号和密码,就不读邮箱,然后再结合Mysql判断,然后返回客户端“1”或者“0”,就可以在客户端判断登录是否正确。

所以搞明白这一点,之后的登录,注册什么的,也就不麻烦了,依葫芦画瓢的事了。

那对于下一步就是主界面的创建,有想法了,实现起来应该也不是很难,最难的应该是后面聊天那里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值