这篇“如何使用Java实现一个验证服务器登录系统”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何使用Java实现一个验证服务器登录系统”文章吧。
一、前言
代码全部由自己所写,作者是一名小白请多多包涵,如果代码有什么不好的地方大佬们可以指出问题
单独写一个这样简易的登录是因为比较方便,由于我尝试了多次在写好的程序内直接写这个登录系统测试,很麻烦、不方便,所以单独写出了这套代码,个人觉得这样把写好的程序放进去修改就比较方便多了
二、登录系统服务端
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生成账号数据");
}
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已经执行");
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){
// 结束循环 断开连接
WhileThread = false;
inputStream.close();
outputStream.close();
accept.close();
}
}
}catch(IOException e){
}
}
}).start();
}
}
}
三、登录系统客户端
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实现一个验证服务器登录系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。