日志文章

2019-12-28 aflfte2011

TCP实现多用户登录

package com.aflfte.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;

/**
 * 多用户登录
 * @author root
 *客户端
 */
public class LoginMultiClient {
    public static void main(String[] args) throws Exception {
        System.out.println("-----Client-----");
        
         //1、建立连接:使用Scoket创建客户端 +指定服务器地址和端口
         Socket client=new Socket("127.0.0.1",8888);
         //2、操作:输入输出流操作
         new Send(client).send();
         new Receive(client).receive();
         //3、释放资源
         client.close();
    }
    //发送
    static class Send{
        private Socket client;
        private DataOutputStream dos;
        private BufferedReader console;
        private String msg;
         public Send(Socket client) {
            super();
            console=new BufferedReader(new InputStreamReader(System.in));
            this.msg=init();
            this.client = client;
            try {
                dos=new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        public String init() {
             try {
                System.out.println("请输入用户名:");
                String uname=console.readLine();
                System.out.println("请输入密码:");
                String upwd=console.readLine();
                return "uname="+uname+"&upwd="+upwd;
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
            return "";
        }
        public void send() {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        
        
    }
    //接收
    static class Receive{
        private DataInputStream dis;
        private Socket client;
        
         public Receive(Socket client) {
            super();
            this.client = client;
            try {
                dis=new DataInputStream(client.getInputStream());
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        public void receive() {
            String datas;
            try {
                datas = dis.readUTF();
                System.out.println(datas);
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
        }

        
    }
}

package com.aflfte.tcp;
/**
 * 模拟实现多个客户端登录
 * 服务端
 * @author root
 *
 */

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

public class LoginMultiServer {
    public static void main(String[] args) throws Exception {
        System.out.println("----Server--------");
        ServerSocket server=new ServerSocket(8888);
        boolean isRunning=true;
        while(isRunning) {
            Socket client =server.accept();
            System.out.println("一个客户端建立了链接");
            new Thread(new Channel(client)).start();
        }
        server.close();
    }
    

    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) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
                release();
            }
        }
        @Override
        public void run() {
            String uname="";
            String upwd="";
            String[] dataArray=receive().split("&");
            for(String userinfo:dataArray) {
                String[] uinfo=userinfo.split("=");
                if(uinfo[0].equals("uname")) {
                    System.out.println("uname-->"+uinfo[1]);
                    uname=uinfo[1];
                }else if(uinfo[0].equals("upwd")) {
                    System.out.println("upwd-->"+uinfo[1]);
                    upwd=uinfo[1];
                }
            }
            if(uname.equals("xiaolei")&&upwd.equals("xiaolei")) {
                send("登录成功! 欢迎回来!");
            }else {
                send("用户名或密码错误!");
            }
            release();
        }
        //接收数据
        private String receive() {
        String datas="";
        try {
            datas = dis.readUTF();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return datas;
        }
        //返回数据
        private void send(String msg) {
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        //释放资源
        private void release() {
            try {
                if(dos!=null) {
                    dos.close();
                }
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
            try {
                if(dis!=null) {
                    dis.close();
                }
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            try {
                if(client!=null) {
                    client.close();
                }
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }
    
}


« TCP聊天室练习实例 | TCP实现上传文件实例»