启动页面:
public class TestStart {
public static void main(String[] args) throws Exception {
TestServerSocket ts = new TestServerSocket();
new Thread(()-> {
try {
ts.reg();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
ts.login();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
服务器页面:
public class TestServerSocket {
private Properties userPro;
public TestServerSocket(){
File file = new File("Files\\userPro.properties");
userPro = new Properties();
try {
userPro.load(new FileReader(file));
} catch (Exception e) {
e.printStackTrace();
}
}
public void reg() throws Exception{
ServerSocket server = new ServerSocket(2000);
System.out.println("注册线程启动");
while(true){
Socket client = server.accept();
new RegThread(client , userPro).start();
}
}
public void login() throws Exception{
ServerSocket server = new ServerSocket(4000);
System.out.println("登录线程启动");
while(true){
Socket client = server.accept();
new LoginThread(client , userPro).start();
}
}
}
注册线程:
public class RegThread extends Thread{
Socket client;
Properties userPro;
public RegThread(Socket client, Properties userPro) {
super();
this.client = client;
this.userPro = userPro;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
String message = br.readLine();
String[] ss = message.split("#");
String name = ss[0];
String password = ss[1];
if(userPro.containsKey(name)){
pw.println("用户名已存在");
pw.flush();
}else{
userPro.put(name, password);
pw.println("注册成功");
userPro.store(new FileWriter("Files\\userPro.properties"), null);
}
pw.close();
br.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
登录线程:
public class LoginThread extends Thread{
Socket client;
Properties userPro;
public LoginThread(Socket client, Properties userPro) {
super();
this.client = client;
this.userPro = userPro;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
String message = br.readLine();
String[] ss = message.split("#");
String name = ss[0];
String password = ss[1];
if(userPro.containsKey(name)){
if(userPro.get(name).equals(password)){
pw.println("登录成功");
pw.flush();
}
else{
pw.println("密码输入错误");
pw.flush();
}
}else{
pw.println("用户名出输入错误");
pw.flush();
}
pw.close();
br.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
启动页面2:
public class TestMnue {
public static void main(String[] args) throws Exception {
TestSocket ts = new TestSocket();
int n;
do{
Scanner sc = new Scanner(System.in);
System.out.println("请输入操作编号:1.注册2.登录 0.退出");
n = sc.nextInt();
switch (n) {
case 1:
ts.reg();
break;
case 2:
ts.login();
break;
default:
break;
}
}while(n != 0);
System.out.println("欢迎下次再来");
}
}
客户端:
public class TestSocket {
public void reg() throws Exception{
Socket client = new Socket("192.168.1.104",2000);
SocketRegThread t = new SocketRegThread(client);
t.start();
t.join();
}
public void login() throws Exception{
Socket client = new Socket("192.168.1.104",4000);
SocketLoginThread t2 = new SocketLoginThread(client);
t2.start();
t2.join();
}
}
注册线程:
public class SocketRegThread extends Thread {
Socket client;
public SocketRegThread(Socket client) {
super();
this.client = client;
}
public void run() {
Scanner sc= new Scanner(System.in);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
System.out.println("请输入注册用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
String combined = name + "#" + password;
pw.println(combined);
pw.flush();
String message = br.readLine();
System.out.println(message);
pw.close();
br.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
登录线程:
public class SocketLoginThread extends Thread {
Socket client;
public SocketLoginThread(Socket client) {
super();
this.client = client;
}
public void run() {
Scanner sc= new Scanner(System.in);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
String combined = name + "#" + password;
pw.println(combined);
pw.flush();
String message = br.readLine();
System.out.println(message);
pw.close();
br.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}