教你用Java驗證服務(wù)器登錄系統(tǒng)
一、前言
代碼全部由自己所寫,作者是一名小白請多多包涵,如果代碼有什么不好的地方大佬們可以指出問題
單獨寫一個這樣簡易的登錄是因為比較方便,由于我嘗試了多次在寫好的程序內(nèi)直接寫這個登錄系統(tǒng)測試,很麻煩、不方便,所以單獨寫出了這套代碼,個人覺得這樣把寫好的程序放進去修改就比較方便多了
二、登錄系統(tǒng)服務(wù)端
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class ServerLogin { public static void main(String[]args)throws IOException{ Server(); } public static void Server()throws IOException{ String name = "AccountPassword.txt"; String path = System.getProperty("user.dir")+"\\"+name; File file = new File(path); if (!file.exists()){ BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile())); file.createNewFile(); bw.write("1391634154--123456"); bw.newLine(); bw.write("654321--123"); bw.flush(); bw.close(); System.out.println("Server生成賬號數(shù)據(jù)"); } ServerSocket server = new ServerSocket(8848); System.out.println("端口開啟成功"); Object obj = new Object(); while(true){ Socket accept = server.accept(); new Thread(new Runnable() { @Override public void run() { int A = 0; boolean ServerStart = false; boolean WhileThread = true; int len = 0; try{ InputStream inputStream = accept.getInputStream(); OutputStream outputStream = accept.getOutputStream(); while(WhileThread){ if (ServerStart==false){ System.out.println("Server=false已經(jīng)執(zhí)行"); BufferedReader br = new BufferedReader(new FileReader(file.getAbsoluteFile())); byte[] bytes = new byte[1024]; len = inputStream.read(bytes); String User = new String(bytes,0,len); len = 0; String Line; while((Line = br.readLine())!=null){ if (Line.equals(User)){ System.out.println("正確"+Thread.currentThread().getName()+"-->User:"+User); outputStream.write("true".getBytes()); ServerStart = true; break; } if (!Line.equals(User)){ A++; System.out.println("失敗"+Thread.currentThread().getName()+"-->User:"+User); outputStream.write("false".getBytes()); break; } } } if (A==3){ // 結(jié)束循環(huán) 斷開連接 WhileThread = false; inputStream.close(); outputStream.close(); accept.close(); } } }catch(IOException e){ } } }).start(); } } }
三、登錄系統(tǒng)客戶端
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class ClienteLogin { static Scanner sc = new Scanner(System.in); static String Server = "false"; public static void main(String[]args)throws IOException{ Cliente(); } public static void Cliente()throws IOException{ int ClienteOff = 0; System.out.println("ClienteOn"); System.out.print("IP:"); String next = sc.next(); System.out.print("Port:"); int Port = sc.nextInt(); Socket socket = new Socket(next,Port); InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); byte[] bytes = new byte[1024]; while(true){ if (ClienteOff == 3){ break; } ClienteOff++; if (Server.equals("false")){ System.out.print("賬號:"); String User = sc.next(); System.out.print("密碼:"); String Password = sc.next(); String AccountPassword = User+"--"+Password; outputStream.write(AccountPassword.getBytes()); int len = inputStream.read(bytes); Server = new String(bytes,0,len); len = 0; if (Server.equals("false")){ System.out.println("登錄失敗,賬號或密碼錯誤"); }else if (Server.equals("true")){ System.out.println("登錄成功"); } } } }
到此這篇關(guān)于教你用Java驗證服務(wù)器登錄系統(tǒng)的文章就介紹到這了,更多相關(guān)Java驗證服務(wù)器登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+feign+Hystrix整合(親測有效)
本文主要介紹了springboot+feign+Hystrix整合,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11SpringBoot自定義FailureAnalyzer詳解
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer詳解,FailureAnalyzer是一種在啟動時攔截?exception?并將其轉(zhuǎn)換為?human-readable?消息的好方法,包含在故障分析中,需要的朋友可以參考下2023-11-11詳解spring-boot actuator(監(jiān)控)配置和使用
本篇文章主要介紹了spring-boot actuator(監(jiān)控)配置和使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Java?Web開發(fā)常用框架Spring?MVC?Struts示例解析
這篇文章主要為大家介紹了Java?Web開發(fā)常用框架Spring?MVC?Struts示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解
這篇文章主要介紹了SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02java中httpclient封裝post請求和get的請求實例
這篇文章主要介紹了java中httpclient封裝post請求和get的請求實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10