This article shares the specific implementation code for java client login server username verification for your reference. The specific content is as follows
The client enters the user name through the keyboard, and the server verifies the user name.
If the user name exists, the server shows that xxx is logged in, and the client shows xxx, welcome to log in.
If the username does not exist, the server displays xxx to try to log in, the client displays xxx, and the username does not exist.
Log in at most three times to prevent violent login.
import java.io.*; import java.net.*; /* *Client*/ class client { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.33.1",10008);//Create a service BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//Read the keyboard and enter the user name PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//Write the BufferedReader to the server after reading the data bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//Read the data returned by the client into for(int x = 0;x < 3; x ++)//Login only 3 times to set { String line = bufr.readLine();//Read the user name pw.println(line); if(line == null)//Terminate break with an empty user name; pw.println(line); String info = bufin.readLine();//Read the data returned by the server System.out.println("Server info:"+info); if(info.contains("Welcome to log in"))//User login terminate break; } bufr.close(); s.close(); } } /* *Server*/ class ServerThread implements Runnable { private Socket s; ServerThread(Socket s) { this.s = s; } public void run() { String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"...........connect"); try { for(int x = 0;x < 3;x ++) { BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//Read the data sent by the client String name = bufin.readLine(); if(name == null) break; BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));//Read the user account that has been deposited, which was originally to read the database, so I wrote a text here PrintWriter out = new PrintWriter(s.getOutputStream(),true);//Write to the stream, and the server writes String line = null; boolean flag = false;//Judge tag while((line = bufr.readLine())!= null)//Read data in the database (Use.txt) { if(line.equals(name))//If the database and read username are the same, terminate { flag = true; break; } } if(flag) { System.out.println(name+":logined"); out.println(name+":welcome to log in"); break; } else { System.out.println(name+":try to log in"); out.println(name+":user name does not exist"); } } s.close(); } catch (Exception e) { throw new RuntimeException("Verification failed"); } } } class server { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10008);//Create service while (true) { Socket s = ss.accept();//Receive data sent by the client new Thread(new ServerThread(s)).start();//Start thread} } }Print result:
user.txt
The above is all about this article, I hope it will be helpful to everyone's learning.