TCP模拟用户登陆

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

/*
 * 客户端
 * 
 * */
public class Client {
	public static void main(String[] args) throws Exception {
		// 创建客户端Socket对象
		Socket s = new Socket(InetAddress.getByName("LuPC"), 17660);

		// 接收键盘录入
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入用户名:");
		String uname = br.readLine();
		System.out.println("请输入用户密码:");
		String pword = br.readLine();

		// 创建输出流对象
		PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
		pw.println(uname);
		pw.println(pword);

		// 创建输入流对象
		BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
		// 输出结果
		System.out.println(br2.readLine());

		// 释放内存
		s.close();
	}
}

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 Server {
	public static void main(String[] args) throws IOException {
		// 创建接收端Socket对象
		ServerSocket ss = new ServerSocket(17660);

		// 监听
		Socket socket = ss.accept();

		// 创建输入流对象
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String name = br.readLine();
		String pass = br.readLine();

		// 创建对象
		UserDB userDB = new UserDB();
		List<User> list = userDB.getList();
		User user = new User(name, pass);

		boolean flag = false;
		// 判断
		if (list.contains(user)) {
			flag = true;
		}

		// 创建输出流对象
		PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);

		// 传输结果
		if (flag) {
			pw.println("登陆成功");
		} else {
			pw.println("登录失败");
		}

		// 释放资源
		socket.close();
	}
}

public class User {
	private String uname;
	private String pword;

	public User() {
		super();
	}

	public User(String uname, String pword) {
		super();
		this.uname = uname;
		this.pword = pword;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getPword() {
		return pword;
	}

	public void setPword(String pword) {
		this.pword = pword;
	}

	@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 (pword == null) {
			if (other.pword != null)
				return false;
		} else if (!pword.equals(other.pword))
			return false;
		if (uname == null) {
			if (other.uname != null)
				return false;
		} else if (!uname.equals(other.uname))
			return false;
		return true;
	}

}

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

public class UserDB {
	private static List<User> list = new ArrayList<User>();

	static {
		list.add(new User("timo", "123"));
		list.add(new User("masteryi", "456"));
	}

	public static List<User> getList() {
		return list;
	}

	public static void setList(List<User> list) {
		UserDB.list = list;
	}

}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭