package com.tz.util.screen;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 客服端通过键盘录入用户名 服务端对这个用户校验 如果该用户存在,在服务端显示xxx,已登录 如果该用户存在,在服务端显示xxx,尝试登陆
* 并在客服端显示xxx,该用户不存在。 最多登录三次
*
* @author Administrator
*
*/
public class LoginClient {
public static void main(String[] args) throws Exception, Exception {
Socket s = new Socket("192.168.1.254", 10007);
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
for (int i = 0; i < 3; i++) {
String line = bufr.readLine();
if (line == null) {
break;
}
out.println(line);
String info = bufIn.readLine();
System.out.println(info);
if (info.contains("")) {
break;
}
}
bufr.close();
s.close();
}
}
class UserThread implements Runnable {
private Socket s;
UserThread(Socket s) {
this.s = s;
}
@Override
public void run() {
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "连接");
try {
for (int i = 0; i < 3; i++) {
BufferedReader bufIn = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String name = bufIn.readLine();
BufferedReader bufr = new BufferedReader(new FileReader(
"use.txt"));
String line = null;
boolean flag = false;
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
while ((line = bufr.readLine()) != null) {
if (line.equals(name)) {
flag = true;
break;
}
}
if (name == null) {
break;
}
if (flag) {
System.out.println(name + "已登录");
out.println(name + "欢迎");
} else {
System.out.println(name + "尝试登录");
out.println(name + "暴力");
}
}
s.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
class LoginServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10007);
while (true) {
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}