本帖最后由 黑马连家华 于 2012-8-13 17:19 编辑
模拟用户登录的小程序,只有三次机会
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",12000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
for (int x = 0;x < 3 ;x++ )
{
String line = br.readLine();//读取键盘录入
if (line == null)//当键盘录入为空,退出登录操作,但为什么我的好像没什么效果?还能继续录入
{
break;
}
out.println(line);
String linein = brin.readLine();
System.out.println("Info : "+linein);
if (linein.contains("欢迎"))//得到服务端的反馈,里面包含 欢迎 时,退出登录操作.这个break怎么又好用,而上面的break不起作用?
{
break;
}
}
br.close();
brin.close();
s.close();
}
}
class TCPLogin implements Runnable
{
private Socket s;
TCPLogin(Socket s)
{
this.s = s;
}
public void run()
{
System.out.println(s.getInetAddress().getHostAddress()+".connected");
try
{
for (int x = 0;x < 3;x++)
{
BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brin.readLine();
if (data == null)//得到客户端的数据,当内容为空,结束三次循环,并在下面关闭Socket服务,这个break又不好用了...
{
break;
}
BufferedReader br = new BufferedReader(new FileReader("UserInfo.ini"));
String line = null;
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
boolean flag = false;
while ((line = br.readLine()) != null)
{
if (line.equals(data))
{
flag = true;
break;
}
}
if (flag)
{
System.out.println(data+",已登录");
out.println(data+",欢迎");
break;
}
else
{
System.out.println(data+",尝试登录");
out.println(data+",用户不存在");
}
}
s.close();//循环结束后关闭服务
}
catch (Exception e)
{
throw new RuntimeException("用户校验失败");
}
}
}
class TCPServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(12000);
while (true)
{
Socket s = ss.accept();
new Thread(new TCPLogin(s)).start();
}
}
}
//找了N遍,还是不知道为什么,别看代码有点长,其实很简单,我觉得一定是我哪里马虎了,或者有其它知识点我没注意到,请大家帮帮忙.这个程序我看两天了
现在我把毕老师的源码贴上
/*
客户端通过键盘录入用户名。
服务端对这个用户名进行校验。
如果该用户存在,在服务端显示xxx,已登陆。
并在客户端显示 xxx,欢迎光临。
如果该用户存在,在服务端显示xxx,尝试登陆。
并在客户端显示 xxx,该用户不存在。
最多就登录三次。
*/
import java.io.*;
import java.net.*;
class LoginClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.254",10008);
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 x=0; x<3; x++)
{
String line = bufr.readLine();
if(line==null)
break;
out.println(line);
String info = bufIn.readLine();
System.out.println("info:"+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 x=0; x<3; x++)
{
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String name = bufIn.readLine();
if(name==null)
break;
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
boolean flag = false;
while((line=bufr.readLine())!=null)
{
if(line.equals(name))
{
flag = true;
break;
}
}
if(flag)
{
System.out.println(name+",已登录");
out.println(name+",欢迎光临");
break;
}
else
{
System.out.println(name+",尝试登录");
out.println(name+",用户名不存在");
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
while(true)
{
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
|
|