public class UserLoginSocket {
/**
* @param args
* @throws Exception
* @throws UnknownHostException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Socket s=new Socket("127.0.0.1",10009);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream());
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<3;x++){
String line=br.readLine();
if(line.equals(null))
break;
out.println(line);
String returnInfo=brin.readLine();
System.out.println("服务端返回的信息是:"+returnInfo);
if(returnInfo.contains("欢迎"))
break;
}
br.close();
s.close();
}
}
public class UserLoginServer implements Runnable{
private Socket s;
public UserLoginServer(Socket s){
this.s=s;
}
@Override
public void run() {
// TODO Auto-generated method stub
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"........已连接上");
try{
for(int x=0;x<3;x++){
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=br.readLine();
if(name==null)
break;
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader brFile=new BufferedReader(new FileReader("E:\\UserLogin.txt"));
String line=null;
boolean flag=false;
while((line=brFile.readLine())!=null){
if(line.equals(name)){
flag=true;
break;
}
}
if(flag){
System.out.println(name+",已登录");
pw.write(name+"欢迎光临");
}else{
System.out.println(name+",尝试登陆");
pw.write(name+",用户名不存在");
}
}
s.close();
}catch(Exception e){
throw new RuntimeException(ip+"校验失败");
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ServerSocket ss=new ServerSocket(10009);
while(true){
Socket s=ss.accept();
new Thread(new UserLoginServer(s)).start();
}
}
}
请各位看一下这是什么原因?
|
|