概述
多个客户端访问服务端进行注册和登录,注册和登录功能以多线程的方式实现
- Server类,服务端类
import java.io.File;
import java.io.FileReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
/**/
public class Server {
private Properties userPros;//Map体系下的集合,其键值对都是String类型
public Server() {
userPros = new Properties();
File file = new File("Files/userPros.properties");//用户信息保存在该文件中
if(file.exists()) {//如果文件存在,则载入数据
try {
userPros.load(new FileReader(file));
} catch (Exception e) {
System.out.println("文件加载失败");
}
}
}
//注册功能
public void registerUser() {
try {
ServerSocket server = new ServerSocket(9988);//1.创建服务端,端口号为9999
System.out.println("注册线程启动。。。");
while(true) {
Socket client = server.accept();//2.接收客户端
new RegThread(client , userPros).start();//启动注册线程
}
} catch (Exception e) {
e.printStackTrace();
}
//5.关闭
}
//登录功能
public void login() {
try {
ServerSocket server = new ServerSocket(8888);//1.创建服务端,端口号为8899
System.out.println("登录线程启动。。。");
while(true) {
Socket client = server.accept();//2.接收客户端
LoginThread Login = new LoginThread(client , userPros);//3.客户端和文件传入线程
Login.start();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
- RegThread类,注册功能的多线程类,继承Thread类
import java.net.Socket;
import java.util.Properties;
import java.io.*;
public class RegThread extends Thread{
private Socket client;
private Properties userPros;
public RegThread(Socket client , Properties userPros) {
super();
this.client = client;
this.userPros = userPros;
}
@Override
public void run() {//注册
InputStream is;
InputStreamReader isr;
BufferedReader bfr = null;
OutputStream os;
OutputStreamWriter osw;
PrintWriter pw = null;
try {
//3.输入输出流
is = client.getInputStream();
isr = new InputStreamReader(is,"UTF-8");
bfr = new BufferedReader(isr);
os = client.getOutputStream();
osw = new OutputStreamWriter(os,"UTF-8");
pw = new PrintWriter(osw);
//4.读入数据
String userinfo = bfr.readLine();
String[] users = userinfo.split("#");
String userName = users[0];
String passWord = users[1];
//判断用户是否已存在
if(userPros.containsKey(userName)) {
pw.println("该用户名已存在");
pw.flush();
}else {
//存在则存入集合
userPros.setProperty(userName, passWord);
//存入到硬盘
userPros.store(new FileWriter("Files/userPros.properties"), null);
pw.println("注册成功!");
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//5.关闭
bfr.close();
pw.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- LoginThread类,登录功能的多线程类,继承Thread类
import java.net.Socket;
import java.util.Properties;
import java.io.*;
public class LoginThread extends Thread {
private Socket client;
private Properties userPros;
public LoginThread(Socket client , Properties userPros) {
this.client = client;
this.userPros = userPros;
}
//登录线程
public void run() {
//3.输入输出流
InputStream is;
InputStreamReader isr;
BufferedReader bfr = null;
OutputStream os;
OutputStreamWriter osw;
PrintWriter pw = null;
try {
is = client.getInputStream();
isr = new InputStreamReader(is , "UTF-8");
bfr = new BufferedReader(isr);
os = client.getOutputStream();
osw = new OutputStreamWriter(os , "UTF-8");
pw = new PrintWriter(osw);
//4.接收数据
String userinfo = bfr.readLine();
String[] users = userinfo.split("#");
String userName = users[0];
String passWord = users[1];
//验证用户名密码
if(userPros.containsKey(userName)) {
if(userPros.getProperty(userName).equals(passWord)) {
pw.println("登录成功");
pw.flush();
}else {
pw.println("密码错误,登录失败");
pw.flush();
}
}else {
pw.println("用户名错误,登录失败");
pw.flush();
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//5.关闭资源
try{
bfr.close();
pw.close();
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
- Client类,客户端类,有向服务端发送注册登录请求的功能
import java.net.Socket;
public class Client {
//注册功能
public void Reg() {
try {
//1.连接服务端
Socket client = new Socket("127.0.0.1",9988);
//2.将客户端传给线程
ClientRegThread reg = new ClientRegThread(client);
//3.启动线程
reg.start();
//4.为了避免主线程先执行
reg.join();
} catch (Exception e) {
e.printStackTrace();
}
}
//登录功能
public void Login() {
try {
//1.连接服务端
Socket client = new Socket("127.0.0.1",8888);
//2.将客户端传入线程并启动线程
ClientLoginThread login = new ClientLoginThread(client);
login.start();
//3.为了避免主线程先执行
login.join();
}catch(Exception e) {
e.printStackTrace();
}
}
}
- ClientRegThread类,客户端注册功能的多线程类
import java.net.Socket;
import java.util.Scanner;
import java.io.*;
public class ClientRegThread extends Thread{
private Socket client;
public ClientRegThread(Socket client) {
this.client = client;
}
@Override
public void run() {
Scanner input = new Scanner(System.in);
//输入输出流
InputStream is;
InputStreamReader isr;
BufferedReader bfr = null;
OutputStream os;
OutputStreamWriter osr;
PrintWriter pw = null;
try {
is = client.getInputStream();
isr = new InputStreamReader(is , "UTF-8");
bfr = new BufferedReader(isr);
os = client.getOutputStream();
osr = new OutputStreamWriter(os , "UTf-8");
pw = new PrintWriter(osr);
//整理数据
System.out.println("请输入用户名:");
String userName = input.next();
System.out.println("请输入密码:");
String passWord = input.next();
String userinfo = userName+"#"+passWord;
//发送给服务端
pw.println(userinfo);
pw.flush();
//接收服务端的回复
String msg = bfr.readLine();
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bfr.close();
pw.close();
client.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
- ClientLoginThread类,客户端登录功能的多线程类
import java.net.Socket;
import java.util.Scanner;
import java.io.*;
public class ClientLoginThread extends Thread {
private Socket client;
public ClientLoginThread(Socket client) {
this.client = client;
}
public void run() {
Scanner input = new Scanner(System.in);
//3.输入输出流
InputStream is;
InputStreamReader isr;
BufferedReader bfr = null;
OutputStream os;
OutputStreamWriter osw;
PrintWriter pw =null;
try {
is = client.getInputStream();
isr = new InputStreamReader(is , "UTF-8");
bfr = new BufferedReader(isr);
os = client.getOutputStream();
osw = new OutputStreamWriter(os , "UTF-8");
pw = new PrintWriter(osw);
//整理数据
System.out.println("请输入用户名:");
String userName = input.next();
System.out.println("请输入密码:");
String passWord = input.next();
String userinfo = userName+"#"+passWord;
//发送数据
pw.println(userinfo);
pw.flush();
//接收回复
String msg = bfr.readLine();
System.out.println(msg);
}catch(IOException e) {
e.printStackTrace();
}finally {
//关闭资源
try {
bfr.close();
pw.close();
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
- StartServer类,服务端的启动类,启动服务端和注册登录功能
public class StartServer {
public static void main(String[] args) {
Server server = new Server();
//启动服务
//匿名内部类
new Thread() {
public void run() {
server.registerUser();
}
}.start();
new Thread() {
public void run() {
server.login();
}
}.start();
}
}
- StartServer类,客户端的启动类,启动注册登录功能的使用
import java.util.Scanner;
public class StartMeun {
public static void main(String[] args) {
Client client = new Client();
Scanner input = new Scanner(System.in);
int choise = 0;
do {
System.out.println("---欢迎来到TEST---");
System.out.println("---1.注册 2.登录 0.退出---");
System.out.println("请输入选择:");
try {
choise = input.nextInt();
}catch(Exception e) {
System.out.println("请输入整数!");
continue;
}
switch(choise) {
case 1 :
client.Reg();
break;
case 2 :
client.Login();
break;
case 0 :
break;
default:
System.out.println("输入错误,请输入0~2的整数");
break;
}
}while(choise != 0);
}
}
服务端运行结果:
注册线程启动。。。
登录线程启动。。。
客户端运行结果:
---欢迎来到TEST---
---1.注册 2.登录 0.退出---
请输入选择:
1
请输入用户名:
alex
请输入密码:
123
注册成功!
---欢迎来到TEST---
---1.注册 2.登录 0.退出---
请输入选择:
1
请输入用户名:
tom
请输入密码:
123
该用户名已存在
---欢迎来到TEST---
---1.注册 2.登录 0.退出---
请输入选择:
2
请输入用户名:
alex
请输入密码:
123
登录成功
---欢迎来到TEST---
---1.注册 2.登录 0.退出---
请输入选择:
2
请输入用户名:
alex
请输入密码:
12
密码错误,登录失败
---欢迎来到TEST---
---1.注册 2.登录 0.退出---
请输入选择:
0
这是目前学习网络编程的练习,功能还不完善,主要是用于练习对服务端客户端信息交互的理解。
最后
以上就是简单月饼为你收集整理的学习Java第三十四天--网络编程之多线程下的服务端客户端注册登录案例的全部内容,希望文章能够帮你解决学习Java第三十四天--网络编程之多线程下的服务端客户端注册登录案例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复