Java验证服务器登录 教你用Java验证服务器登录系统
JiaDog0 人气:0想了解教你用Java验证服务器登录系统的相关内容吗,JiaDog0在本文为您仔细讲解Java验证服务器登录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java验证服务器登录,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("登录成功"); } } } }
加载全部内容