验证客户端向服务端发送用户名请求登录

标签: 前端学习

客服端向服务端发送用户名请求登录,服务端通过验证,返回"欢迎光临",未通过,用户不存在。
这里可以自己写一个文件当当做后台用户数据,也可以使用数据库。
这里是自己写文件。

服务端

package wanfeng;

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

public class loginserver {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(9000);
        while(true)
        {
            Socket s = ss.accept();
            System.out.println(ss.getInetAddress().getHostAddress()+".....connected");
            new Thread(new UserThread(s)).start();
        }
    }
}
class  UserThread implements Runnable{
    private  Socket s;
    UserThread(Socket s)
    {
        this.s = s;
    }
    public void run()
    {
        try
        {
            for(int x=0; x<3; x++)
            {
                //判断用户名是否为空
                BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String name = bufIn.readLine();
                if(name==null)
                {
                    System.out.println("程序结束 ");
                    break;
                }

                //匹配用户名并实现登陆
                BufferedReader bufr = new BufferedReader(new FileReader("D:\\user\\用户.txt"));
                String line = null;
                PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                boolean b = false;
                while((line=bufr.readLine())!=null)
                {
                    if(line.equals(name))
                    {
                        b = true;
                        break;
                    };
                }
                if(b)
                {
                    System.out.println(name+"已登录");
                    out.println(name+",欢迎光临");
                }
                else
                {
                    System.out.println(name+"尝试登录");
                    out.println(name+",用户不存在");
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
        finally
        {
            try
            {
                if( s!=null)
                    s.close();
            }
            catch (Exception e)
            {
                System.out.println("close:"+e.toString());
            }

        }
    }
}

客户端

package wanfeng;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.*;
import java.io.*;

public class loginClient {
    public static void main(String[] args) throws IOException{
        Socket socket = new Socket("127.0.0.1",9000);

        BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        for(int i=1;i<=3;i++)
        {
            String username=buf.readLine();
            if(username==null)
            {
                break;
            }
            out.println(username);

            String iof=in.readLine();
            System.out.println(iof);
            if(iof.contains("欢迎"))
            {
                break;
            }
        }
        buf.close();
        socket.close();

    }
}

运行效果

在这里插入图片描述

版权声明:本文为s1050712899原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/s1050712899/article/details/108810641