需求:
1.客户端选择登陆或注册操作
2.客户进行注册,服务端将数据存入到文件中
3.客户端进行登陆,服务端验证用户名密码是否正确
服务端采用多线程,将获得的数据写入配置文件中,文件为静态,
客户端采用单线程
1 package com.java.login; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStream; 9 import java.io.OutputStreamWriter; 10 import java.net.InetAddress; 11 import java.net.Socket; 12 import java.net.UnknownHostException; 13 14 /** 15 * 客户端 16 * 17 * @author syousetu 18 * 19 */ 20 public class Client extends Thread { 21 public static void main(String[] args) throws UnknownHostException, IOException { 22 // 开启服务 23 Socket socket = new Socket(InetAddress.getLocalHost(), 9090); 24 25 // 获取socket输出流对象 26 OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream()); 27 28 // 获取从键盘输入流对象 29 BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in)); 30 31 // 获取socket输入流对象 32 BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 33 34 // 注册完成会进行登陆 35 while (true) { 36 System.out.println("请选择操作:A:登陆 B:注册"); 37 String option = keyReader.readLine(); 38 39 if ("a".equalsIgnoreCase(option)) { 40 // 登陆 41 send(socketOut, option, keyReader); 42 // 接受服务器发送过来的信息 43 String answer = socketReader.readLine(); 44 System.out.println(answer); 45 46 } else if ("b".equalsIgnoreCase(option)) { 47 // 注册 48 send(socketOut, option, keyReader); 49 String answer = socketReader.readLine(); 50 System.out.println(answer); 51 52 } 53 54 } 55 56 } 57 58 public static void send(OutputStreamWriter socketOut, String option, BufferedReader keyReader) throws IOException { 59 System.out.println("请输入用户名:"); 60 String name = keyReader.readLine(); 61 System.out.println("请输入密码:"); 62 String password = keyReader.readLine(); 63 64 String info = option + " " + name + " " + password + " "; 65 66 socketOut.write(info); 67 socketOut.flush(); 68 69 } 70 71 }
1 package com.java.login; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.InputStreamReader; 10 import java.io.OutputStreamWriter; 11 import java.net.ServerSocket; 12 import java.net.Socket; 13 import java.util.Properties; 14 15 /** 16 * 服务端 17 * @author syousetu 18 * 19 */ 20 public class Server extends Thread { 21 22 // 获得数据公用一个文件 23 static File file = new File("D:\user.properties"); 24 25 Socket socket; 26 //不同的客户端有不同的socker 27 public Server(Socket socket) { 28 this.socket = socket; 29 } 30 31 static { 32 try { 33 if (!file.exists()) { 34 file.createNewFile(); 35 } 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 @Override 42 public void run() { 43 try { 44 45 // 获取输出流对象 46 OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream()); 47 48 // 获取输入流对象 49 BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 50 while (true) { 51 String info = socketReader.readLine(); 52 String[] data = info.split(" "); 53 54 String option = data[0]; 55 String name = data[1]; 56 String password = data[2]; 57 System.out.println("option:" + option); 58 59 // 判断是登陆还是操作 60 if ("a".equalsIgnoreCase(option)) { 61 // 登陆操作 62 Properties properties = new Properties(); 63 // 加载配置文件 64 properties.load(new FileReader(file)); 65 if (!properties.contains(name)) { 66 socketOut.write("用户名不存在 "); 67 } else if (password.equals(properties.getProperty(name))) { 68 socketOut.write("登陆成功 "); 69 } else { 70 socketOut.write("密码错误 "); 71 } 72 73 } else if ("b".equalsIgnoreCase(option)) { 74 // 进行注册 75 // 创建一个配置文件类 76 Properties properties = new Properties(); 77 // 加载原来的文件 78 properties.load(new FileReader(file)); 79 80 if (properties.contains(name)) { 81 // 用户名已存在 82 socketOut.write("用户名已存在,请重新输入 "); 83 } else { 84 // 新用户注册 85 properties.put(name, password); 86 properties.store(new FileWriter(file), "users"); 87 socketOut.write("注册成功 "); 88 89 } 90 91 } 92 socketOut.flush(); 93 } 94 95 } catch (FileNotFoundException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } catch (IOException e) { 99 // TODO Auto-generated catch block 100 e.printStackTrace(); 101 } 102 103 } 104 105 public static void main(String[] args) throws IOException { 106 // 开启服务 107 ServerSocket serverSocket = new ServerSocket(9090); 108 109 while (true) { 110 // 接受链接 111 Socket socket = serverSocket.accept(); 112 new Server(socket).start(); 113 } 114 115 } 116 117 }