欢迎光临散文网 会员登陆 & 注册

使用TCP实现多用户登录的代码

2020-04-19 22:44 作者:小垃圾kiki  | 我要投稿
package cn.jd.tcp;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/*
 * 模拟登录   多个客户端请求
 * 创建客户端
 * 1.使用Socket创建客户端(这里实际上就是和服务器建立连接)
 * 所以需要指定服务器的地址和端口
 * 2.输入输出流操作
 * 3.释放资源
 */
public class LoginMultiClient {
    public static void main(String[] args) throws UnknownHostException, IOException {
        System.out.println("------client----------");
        // 1.使用Socket创建客户端(这里实际上就是和服务器建立连接)
        Socket client = new Socket("localhost", 8888);
        // 2.输入输出流操作 先请求后响应
        new Send(client).send();
        new Receive(client).receive();
        // 3.释放资源
        client.close();
    }

    // 发送
    static class Send {
        private Socket client;
        private DataOutputStream dos;
        // InputStreamReader是从字节流到字符流的桥:它读取字节,并使用指定的charset将其解码为字符
        private BufferedReader console;
        private String msg;

        public Send(Socket client) {
            console = new BufferedReader(new InputStreamReader(System.in));
            this.msg=init();
            this.client = client;
            try {
                dos = new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private String  init() {
            try {
                System.out.println("请输入用户名:");
                String uname = console.readLine();
                System.out.println("请输入密码:");
                String upwd = console.readLine();
                return  "用户名:" + uname + "&" + "密码:" + upwd;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "";

        }

        public void send() {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    // 接收
    static class Receive {
        private Socket client;
        private DataInputStream dis;

        public Receive(Socket client) {
            this.client = client;
            try {
                dis = new DataInputStream(client.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void receive() {
            String result;
            try {
                result = dis.readUTF();
                System.out.println(result);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 接收数据

        }

    }

}

package cn.jd.tcp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 模拟登录 多个客户端请求
 * 创建服务器
 * 1.指定端口:使用ServerSocket创建服务器
 * 2.阻塞式的等待连接
 * 3.输入输出流操作
 * 4.释放资源
 */
public class LoginMultiServer {
    public static void main(String[] args) throws IOException {
        System.out.println("------server----------");
        // 1.指定端口:使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        boolean isRunning = true;
        // 2.阻塞式等待连接accept
        while (isRunning) {
            // 建立了连接之后启动多线程
            Socket client = server.accept();// 一次accept就是一个连接
            System.out.println("一个客户端建立了连接");
            new Thread(new Channel(client)).start();
        }
        server.close();
    }
    //一个channel就代表一个客户端
    // 内部类可以使用外部类的任何变量和方法
    static class Channel implements Runnable {
        private Socket client;
        // 输入流
        private DataInputStream dis;
        // 输出流
        private DataOutputStream dos;
        //构造器构建好输入流和输出流
        public Channel(Socket client) {
            this.client = client;
            try {
                dis = new DataInputStream(client.getInputStream());
                dos = new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                release();
            }
            // 输出

        }

        // 接收数据
        private String receive() {
            String datas = "";
            try {
                datas = dis.readUTF();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return datas;
        }

        // 释放资源
        private void release() {
            // 4.释放资源
            try {
                if(dos!=null) {
                    dos.close();
                }    
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                if(dis!=null) {
                    dis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(client!=null) {
                    client.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // 发送数据
        private void send(String msg) {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public void run() {

            // 3.输入输出流操作
            String uname = "";
            String upwd = "";
            // 分析
            String[] dataArray = receive().split("&");
            for (String info : dataArray) {
                String[] userInfo = info.split(":");
//            System.out.println(userInfo[0]+"-->"+userInfo[1]);
                if (userInfo[0].equals("用户名")) {
                    System.out.println("你的用户名为:" + userInfo[1]);
                    uname = userInfo[1];
                } else if (userInfo[0].equals("密码")) {
                    System.out.println("你的密码为:" + userInfo[1]);
                    upwd = userInfo[1];
                }
            }

            if (uname.equals("abc") && upwd.equals("123")) {// 成功
                send("登录成功");
            } else {// 失败
                send("用户名和密码错误");
            }
            release();

        }

    }

}

使用TCP实现多用户登录的代码的评论 (共 条)

分享到微博请遵守国家法律