Java Client Login Server user name authentication _java

Source: Internet
Author: User
Tags readline

This example for you to share the Java Client Login Server user name verification of the specific implementation code, for your reference, the specific contents are as follows

The client enters the user name through the keyboard, and the server authenticates the user name.
if the user name exists, the service side shows xxx is logged in, the client shows XXX, welcome to login.
If the username does not exist, the service side shows xxx trying to log in, the client displays XXX, and the username does not exist.
log up to three times to prevent violent landings.

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);//Establish service BufferedReader BUFR = new BufferedReader (new InputStreamReader (system.in));//Read keyboard input username Print Writer pw = new PrintWriter (S.getoutputstream (), true);//Read data to service-side write BufferedReader Bufin = new BufferedReader (New INP Utstreamreader (S.getinputstream ())//Read the data returned by the client for (int x = 0;x < 3 x + +)/Login only 3 times set {String = Bufr.re 
   Adline ()//Read user name Pw.println (line); 
   if (line = = null)//NULL user name to terminate the break; 
   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 login"))//user login to terminate the break; 
  } bufr.close (); 
 S.close (); 
 } * * Service End/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.get 
    InputStream ());//Read the data String name = Bufin.readline () sent by the client; 
    if (name = = null) break;  BufferedReader bufr = new BufferedReader (New FileReader ("User.txt"));/read has been deposited into the user account, read the database, where it is convenient to write a text printwriter out 
    = new PrintWriter (S.getoutputstream (), true);/write stream, the server writes String line = null; Boolean flag = false;//Judge Mark while (line = Bufr.readline ())!= null)//Read database (Use.txt) data {if (line.equals) (Nam 
      e)//If the database and read user names are the same, terminate {flag = true; 
     Break 
      } if (flag) {System.out.println (name+ ": Logged in"); 
      Out.println (name+ ": Welcome to the Landing"); 
     Break 
     else {System.out.println (name+ ": try to login"); 
    Out.println (name+ ": username does not exist"); 
  } s.close (); catch (Exception e) {throw new RuntimeException ("validation failed"); }} Class Server {public static void main (string[] args) throws Exception {ServerSocket ss = new Serversoc Ket (10008);//Establish service while (true) {Socket s = ss.accept ();//Receive Client Data new Thread (new Serverthread (s)). Start (); 
 Open Thread}}}

Print results:

User.txt


The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.