tcp编程:多用户登录

package com.sundear.demo.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。 释放资源
 */
public class LoginMultiServer {
    public static void main(String[] args) throws IOException {
        System.out.println("---server---");
        //指定端口 使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        boolean isRunning =true;
        while (isRunning) {
            //阻塞式等待连接 accept
            Socket client = server.accept();
            System.out.println("一个客户端建立了连接");
            new Thread(new Channel(client)).start();
        }
        server.close();
    }

   static class Channel implements Runnable{
       private Socket client;
       public  DataInputStream dis;
       public 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();
               relesae();
           }


       }
       private String receive(){
           String datas ="";
           try{
              datas = dis.readUTF();
           }catch (Exception e){
               e.printStackTrace();
           }
           return datas;
       }

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

       }
       private void relesae(){
           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();
           }
       }
       @Override
       public void run() {
           //操作 输入输出流操作
           String username = "";
           String password = "";
           String[] datas = receive().split("&");
           for (String info : datas) {
               String[] user = info.split("=");
               if (user[0].equals("username")) {
                   System.out.println("你的用户名为" + user[1]);
                   username = user[1];
               } else if (user[0].equals("password")) {
                   System.out.println("你的密码为" + user[1]);
                   password = user[1];
               }
           }

           if (username.equals("root") && password.equals("sundear123")) {
               send("登录成功,欢迎回来");
           } else {
               send("用户名或密码错误");
           }
           relesae();
       }
   }

}

  

 

package com.sundear.demo.tcp;

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

/**
 * 模拟登录:多用户登录
 * 创建客户端
 * 1。建立连接 使用Socket创建客户端+服务的地址和端口
 * 2. 操作 输入输出流操作
 * 3。 释放资源
 */
public class LoginMultiClient {
    public static void main(String[] args) throws IOException {
        System.out.println("---client---");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入用户名");
        String username =br.readLine();
        System.out.println("请输入密码");
        String password =br.readLine();
        Socket client =new Socket("localhost",8888);
        new Send(client).send("username="+username+"&"+"password="+password);
        new Receive(client).receive();
        client.close();

    }
    static class Send{
        private Socket client;
        private  DataOutputStream dos;
        public Send(Socket client){
            this.client=client;
            try {
                dos =new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void send(String msg){
            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();
             }
         }
    }
}

  

posted @ 2021-07-15 11:01  下饭  阅读(125)  评论(0编辑  收藏  举报