一、前言
代碼全部由自己所寫,作者是一名小白請多多包涵,如果代碼有什么不好的地方大佬們可以指出問題
單獨寫一個這樣簡易的登錄是因為比較方便,由于我嘗試了多次在寫好的程序內直接寫這個登錄系統(tǒng)測試,很麻煩、不方便,所以單獨寫出了這套代碼,個人覺得這樣把寫好的程序放進去修改就比較方便多了
二、登錄系統(tǒng)服務端
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ù)據");
}
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已經執(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){
// 結束循環(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("登錄成功");
}
}
}
}
文章到此,以上關于使用Java實現(xiàn)驗證服務器登錄系統(tǒng)的文章就介紹結束了,想要了解更多相關Java的內容,可以瀏覽W3Cschool其他相關技術文章,希望本篇文章能夠對大家的學習有所幫助!