案例代码1
需求:
客户端:
1.提示用户输入用户名和密码,将用户输入的用户名和密码发送给服务端
2.接收服务端验证完用户名和密码的结果
服务端:
1.接收客户端发送过来的用户名和密码
2.如果用户名不是itheima或者 密码不是123456,就向客户端写入”登录失败”
否则向客户端写入登录成功
ClientTest.java
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 void main(String[] args) throws IOException {
//创建客户端Socket对象
//Socket s = new Socket(InetAddress.getByName("itheima"),8888);
Socket s = new Socket("itheima",8888);//Socket中的一个构造方法,将传进的主机名字符串,从而调用(InetAddress.getByName("itheima")获取所在的IP地址
//获取用户名和密码
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String username = br.readLine();
System.out.println("请输入密码:");
String password = br.readLine();
//获取输出流对象
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
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();
}
}
ServerTest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
public static void main(String[] args) throws IOException {
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听
Socket s = ss.accept();//获取客户端传来的数据对象
//获取输入流对象
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取用户名和密码
String username = br.readLine();
String password = br.readLine();
//判断用户名和密码是否正确
boolean flag = false;
if("itheima".equals(username) && "123456".equals(password)) {
flag = true;
}
//获取输出流对象
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//返回判断信息
if(flag) {
out.println("登陆成功");
}
else {
out.println("登陆失败");
}
//释放资源
s.close();
//ss.close();//服务器一般不关闭
}
}
案例代码2
对案例代码1进行改写
需求:
将用户名和密码封装到一个User类中,提供对应的构造方法和getter/setter方法
新建一个UserDB类里面定义一个集合,在集合中添加以下User对象
new User("zhangsan","123456");
new User("lisi","654321");
new User("itheima","itheima");
new User("admin","password");
客户端:
1.提示用户输入用户名和密码,将用户输入的用户名和密码发送给服务端
2.接收服务端验证完用户名和密码的结果
服务端:
1.服务端将客户端发送过来的用户名密码封装成User对象
2.集合中如果包括这个User对象,想客户端写入” 登录成功”
否则向客户端写入”登录失败”
ClientTest.java
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 void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("itheima",8888);
//获取用户名和密码
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();
}
}
ServerTest.java
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;
public class ServerTest {
public static void main(String[] args) throws IOException {
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听
Socket s = ss.accept();
//获取输入流对象
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取用户名和密码
String username = br.readLine();
String password = br.readLine();
//判断用户名和密码是否正确
boolean flag = false;
/*if("itheima".equals(username) && "123456".equals(password)) {
flag = true;
}*/
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();
//ss.close();//服务器一般不关闭
}
}
User.java
public class User{
private String username;
private String password;
public User(){
super();
}
public User(String username,String password){
super();
this.username = username;
this.password = password;
}
public String getUsername(){
return username;
}
public void setUsername(){
this.username= username;
}
public String getPassword(){
return password;
}
public void setPassword(){
this.password = password;
}
//重写equails,而contains方法中使用了equails方法。
@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.equal(other.password)))
return false;
if(username!== null){
if(other.username!== null)
return false;
}else if(!username.equal(other.username)))
return false;
}
}
UsereDB.java
/*
存储用户对象,相当于模拟数据库
*/
import java.util.ArrayList;
import java.util.List;
public class UserDao{
private static List<User> users = new List<User>();
static {
users.add(new User("zhangsan","123456"));
users.add(new User("lisi","12345"));
users.add(new User("wagnwu","1234"));
users.add(new User("itheima","itheima"));
}
public static List<User> getUsers(){
return Users;
}
}