基于TCP实现多用户登录

基于TCP实现多用户登录

  • 创建服务器
  1. 指定端口 使用ServerSocket创建服务器
  2. 阻塞式等待连接 accept
  3. 操作: 输入输出流操作
  4. 释放资源
  • 创建客户端
  1. 建立连接: 使用Socket创建客户端 +服务的地址和端口
  2. 操作: 输入输出流操作
  3. 释放资源

Server

package com.msl.tcp;

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

/**
 * 模拟登录 多个客户端请求
 * 创建服务器
 * 1、指定端口 使用ServerSocket创建服务器
 * 2、阻塞式等待连接 accept
 * 3、操作: 输入输出流操作
 * 4、释放资源 
 * @author Senley
 *
 */
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(); 
            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) {
                e.printStackTrace();
            }
            return datas;
        }
        //释放资源
        private void release() {
            // 4、释放资源 
            try {
                if(null != dos) {
                    dos.close();                    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null != dis) {
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null != client) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //发送数据
        private void send(String msg) {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {                        
            // 3、操作: 输入输出流操作                    
            String uname ="";
            String upwd ="";
            //分析
            String[] dataArray = receive().split("&");
            for(String info:dataArray) {
                    String[] userInfo =info.split("=");
                    if(userInfo[0].equals("uname")) {
                        System.out.println("你的用户名为:"+userInfo[1]);
                        uname = userInfo[1];
                    }else if(userInfo[0].equals("upwd")) {
                        System.out.println("你的密码为:"+userInfo[1]);
                        upwd = userInfo[1];
                    }                
            }                    
            if(uname.equals("senley") && upwd.equals("123")) { //成功
                send("登录成功,欢迎回来");
            }else { //失败
                send("用户名或密码错误");
            }        
            release();        
        }    
    }
}

Client

package com.msl.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、释放资源 
 * @author Senley
 *
 */
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();        
        client.close();
    }
    //发送
    static class Send{
        private Socket client;
        private DataOutputStream dos;
        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.print("请输入用户名:");
                String uname =console.readLine();
                System.out.print("请输入密码:");
                String upwd =console.readLine();
                return "uname="+uname+"&"+"upwd="+upwd;
            } catch (IOException e) {
                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) {
                e.printStackTrace();
            }        
        }
    }
}

示例结果如下:

LoginMultiServer

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021-2022 Senley
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信