客户端和服务端的消息传递

客户端和服务端的消息传递
        package com.NetDemo02;
		//客户端
        import java.io.*;
        import java.net.Socket;
        import java.util.Scanner;
        public class ClicentService {
            public static void main(String[] args) throws IOException {
                //建立网络连接Socket的本机地址,和端口号
                Socket socket =new Socket("192.168.201.23",8080);
                //将控制台输入的信息接收
                Scanner scanner = new Scanner(System.in);
                System.out.println("请录入您的账号:");
                String name = scanner.next();
                System.out.println("请录入您的密码:");
                String pwd = scanner.next();

                Information information =new Information(name,pwd);
                OutputStream output =socket.getOutputStream();
                ObjectOutputStream oops = new ObjectOutputStream(output);
                oops.writeObject(information);
        //服务器的接收流
                InputStream inputStream  = socket.getInputStream();
                DataInputStream dataInputStream = new DataInputStream(inputStream);
                if(dataInputStream.readBoolean()){
                    System.out.println("欢迎使用登录");
                }else {
                    System.out.println("账号或密码错误");
                }
                inputStream.close();
                oops.close();
                output.close();
                socket.close();
            }
        }

        package com.NetDemo02;
		//服务器端
        import java.io.IOException;
        import java.net.InetAddress;
        import java.net.ServerSocket;
        import java.net.Socket;

        public class ServiceDemo {
            public static void main(String[] args) {
                try {
                    ServerSocket serverSocket = null;
                    serverSocket = new ServerSocket(8080);
                    //历史浏览记录
                    int count = 0;
                    //进行线程阻塞;等待客户端的服务请求
                    //通过一个while方法对服务器进行循环阻塞
                    while (true) {
                        Socket accept = serverSocket.accept();
                        //必须对线程进行启动
                        new ServiceTreand(accept).start();
                        count++;
                        System.out.println("记录的IP:"+InetAddress.getLocalHost()+";访问的个数:"+count);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            }



        package com.NetDemo02;
		//自定义方法
        import java.io.Serializable;

        public class Information implements Serializable {
            private static final long serialVersionUID = 1039520093218203872L;
            private String name;
            private String pwd;

            public Information(String name, String pwd) {
                this.name = name;
                this.pwd = 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;
            }

        }

        package com.NetDemo02;
		//线程端
        import java.io.*;
        import java.net.Socket;

        public class ServiceTreand extends Thread{
            Socket accept = null;
            DataOutputStream dataOutputStream = null;
            OutputStream outputStream= null;
            ObjectInputStream obips=null;
            InputStream inputStream = null;
            public ServiceTreand(Socket accept) {
                this.accept = accept;
            }

            @Override
            public void run() {
                try {
                    inputStream =accept.getInputStream();
                    obips = new ObjectInputStream(inputStream);
                    Information information = (Information)(obips.readObject());
                    //服务器的返回流,返回boolean值;客户端进行判断
                    outputStream = accept.getOutputStream();
                    dataOutputStream = new DataOutputStream(outputStream);
                    if (information.getName().equals("丽丽")||information.getPwd().equals("123123")){
                        dataOutputStream.writeBoolean(true);
                    }else {
                        dataOutputStream.writeBoolean(false);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if(dataOutputStream!= null) {
                            dataOutputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        if(outputStream!=null){
                            outputStream.close();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        if(obips!=null){
                            obips.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        if(inputStream!=null){
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                       if(accept!=null){
                           accept.close();
                       }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

posted @ 2022-04-20 21:32  爱豆技术部  阅读(40)  评论(0)    收藏  举报