使用TCP模拟登陆

import java.util.ArrayList;
import java.util.List;


public class UserDB {
//使用Map存储账号密码
private static List<User>users=new ArrayList<User>();
//模拟数据库
static{
//自定义用户密码
users.add(new User("利亚东","123456"));
users.add(new User("超超","654321"));
users.add(new User("彪","123321"));
users.add(new User("???","toString"));
}
public static List<User> getUsers(){
return users;
}
ublic class User {
private String username;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import org.apache.log4j.Logger;
public class ServerTest {

public static void main(String[] args) throws IOException {
//创建服务器
ServerSocket ss=new ServerSocket(1234);
//监听
Socket s=ss.accept();
//获取输入流对象
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取用户名密码
String username=br.readLine();
String password=br.readLine();
boolean flag=false;

List<User> users=UserDB.getUsers();
User user=new User(username,password);
if(users.contains(user)){
//匹配成功
flag=true;
}
//获取输出流对象
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
if(flag){
out.println("登录成功");
}else{
out.println("登录失败");
}
s.close();
}

}
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


//模拟用户登录
public class ClientTest {
public static Logger logger=Logger.getLogger(Liyado.class.getClass());
public static void main(String[] args) throws IOException {
//通过计算机名获取
Socket s=new Socket("SKY-20171211NJY",1234);
//获取用户名密码
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String username=br.readLine();
System.out.println("请输入秘密:");
String password=br.readLine();
//获取输出流对象
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//写出数据
out.println(username);
out.println(password);
//获取输入流对象
BufferedReader serverBr=new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取服务器返回的数据
String result=serverBr.readLine();
System.out.println(result);
//释放资源
s.close();
}

private static class Liyado {
}
}

执行结果

 


猜你喜欢

转载自www.cnblogs.com/liyado/p/10635844.html