黑马程序员技术交流社区
标题:
限制ip怎么做
[打印本页]
作者:
陌路行者
时间:
2013-7-15 15:49
标题:
限制ip怎么做
import java.io.*;
import java.net.*;
class LoginClient
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket("192.168.1.12",8866);
System.out.println("请输入用户名");
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;
}
public void run()
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...........connected");
try
{
for(int i=3;i>0;i--)
{
BufferedReader bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bufr =
new BufferedReader(new FileReader("userInfo.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String name = bufIn.readLine();
if(name==null)
break;
String line = null;
boolean flag = false;
while((line=bufr.readLine())!=null)
{
if(line.equals(name))
flag = true;
break;
}
if(flag)
{
System.out.println("已登陆");
out.println(name+"登陆成功,欢迎光临");
break;
}
else
{
System.out.println(ip+"尝试登陆");
if(i==1)
{
out.println("登陆受限,请30分钟后重试");
}
else
out.println(name+"登陆失败,请检查您的用户名!还剩"+(i-1)+"次机会");
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(8866);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
复制代码
QQ图片20130715141518.jpg
(134.18 KB, 下载次数: 0)
下载附件
2013-7-15 15:49 上传
作者:
许庭洲
时间:
2013-7-18 06:11
值得学习ing!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2