package liuhui.zjzk;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class LoginThread implements Runnable{
private Socket s;
public LoginThread() {
}
public LoginThread(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("G:/user.txt"));
String line = null;
boolean flag = false;
while((line = bufr.readLine()) != null){
if(line.equals(name)){
flag = true;
break;
}
}
// BufferedWriter bufOut = new BufferedWriter(
// new OutputStreamWriter(s.getOutputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
if(flag){
System.out.println("server ::"+name+",登录成功");
out.println(name+",欢迎登录");
break;
}else{
System.out.println("server ::"+name+",尝试成功");
out.println(name+",3次用户登录失败");
}
}
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
使用获取键盘输入流发送不成功。 |
|