一、前言
代码全部由自己所写,作者是一名小白请多多包涵,如果代码有什么不好的地方大佬们可以指出问题
单独写一个这样简易的登录是因为比较方便,由于我尝试了多次在写好的程序内直接写这个登录系统测试,很麻烦、不方便,所以单独写出了这套代码,个人觉得这样把写好的程序放进去修改就比较方便多了
二、登录系统服务端
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
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();
}
}
} |
三、登录系统客户端
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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验证服务器登录系统的文章就介绍到这了,更多相关Java验证服务器登录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_45141116/article/details/115920330
查看更多关于教你用Java验证服务器登录系统的详细内容...