黑马程序员技术交流社区
标题:
客户端并发登陆练习问题?
[打印本页]
作者:
范龙波
时间:
2013-6-14 23:18
标题:
客户端并发登陆练习问题?
import java.net.*;
import java.io.*;
class DengLu
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.0.103",10007);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader bfs=new BufferedReader(new InputStreamReader(s.getInputStream()));
for (int x=0;x<3 ;x++ )
{
String str=bf.readLine();
if (str==null)
break;
out.println(str);
String strd=bfs.readLine();
if (strd.contains("恭喜"))
break;
System.out.println("strd"+strd);
}
}
}
class FuWuQi
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10007);
while (true)
{
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip);
new Thread(new UserThread(s)).start();
}
}
}
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
for (int x=0;x<3 ;x++ )
{
String ip=s.getInetAddress().getHostAddress();
BufferedReader bf=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bfu=new BufferedReader(new FileReader("User.txt"));
PrintWriter out=new PrintWriter(s.getOutputStream());
String str=bf.readLine();
String str1=null;
Boolean b=false;
while ((str1=bfu.readLine())!=null)
{
if (str1.equals(str))
{
b=true;
break;
}
}
if(b)
{
System.out.println(str+"登陆成功");
out.println(str+"恭喜成功进入");
break;
}
/*else
{
System.out.println(str+"有人登陆");
out.println("登陆失败去吧");
}
*/
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException ("连接失败");
}
}
}
//这个代码我看了很多次了,每次都只能输入一次不管输入的是否是已有的名字都只能打印一次,看了很多遍没有找到问题出在哪里?请指教下,谢谢
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2