import java.io.*;
import java.net.*;
class LoginClien
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.2.71",1004);//创建一个连接到指定地址的端口号
BufferedReader bfr=
new BufferedReader(new InputStreamReader(System.in));
//创建一个从服务器获取数据的输入流的缓冲区
BufferedReader bin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=
new PrintWriter(s.getOutputStream(),true);// 通过现有的 OutputStream 创建新的 PrintWriter true代表刷新,
for (int x=0;x<3 ;x++ )
{
String username=bfr.readLine(); //从键盘获取用户名
if(username==null)
{
System.out.println("用户名不能为空!!");
break;
}
pw.println(username);
String info=bin.readLine();
System.out.println(info);
if(info.contains("欢迎"))
break;
}
//System.out.println("系统发现你在恶意登录,现在暂停你的账号1小时");
s.close();
bfr.close();
}
}
class LoginThread implements Runnable
{
private Socket s;
LoginThread(Socket s)
{
this.s=s; }
public void run()
{
String ip=s.getInetAddress().getHostAddress();//获取IP
System.out.println(ip+"...connected");
try
{
for (int x=0;x<3 ;x++ )
{
BufferedReader br=
new BufferedReader(new InputStreamReader(s.getInputStream()));//从套接字获取输入信息
String username=br.readLine();
if(username==null)
break;
BufferedReader bfr=
new BufferedReader(new FileReader("userinfo.txt"));
PrintWriter pw=
new PrintWriter(s.getOutputStream(),true);//从套接字获取输出信息
String line=null;
boolean flag=false;
while ((line=bfr.readLine())!=null)
{
if(username.equals(line))
{
flag=true;
break;
}
}
if(flag!=true)
{
if(x>=2)
pw.println("系统发现你恶意登录,现已暂停你1小时");
pw.println(username+"::"+"登录名有误,情重新登录");
System.out.println("登录名有误,请重新登录");
}
else
{
pw.println(username+"...登录成功,欢迎光临");
System.out.println("登录成功,欢迎光临");
break;
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException("用户登录失败");
}
} } class LoginServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(1004);
while (true)
{
Socket s=ss.accept();
new Thread(new LoginThread(s)).start(); }
}
}
|