黑马程序员技术交流社区
标题:
用tcp实现用户登录不成功的问题
[打印本页]
作者:
江峰渔火
时间:
2014-6-14 15:02
标题:
用tcp实现用户登录不成功的问题
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();
}
}
}
使用获取键盘输入流发送不成功。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2