在看毕老师视频的时候对源代码的一些问题- import java.net.*;
- import java.io.*;
- class LoginThread implements Runnable
- {
- private Socket s;
- public LoginThread(Socket s)
- {
- this.s = s;
- }
- public void run()
- {
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"connecting、、、");
- try
- {
-
- for(int x=0;x<3;x++)
- {
- BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
-
- BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
-
- PrintWriter bufout = new PrintWriter(s.getOutputStream(),true);
-
- String line = bufin.readLine();
-
- if(line==null)
- break;
-
- String username = null;
-
- boolean flag = false;
-
- while((username=bufr.readLine())!=null)
- {
- if(username.equals(line))
- {
- flag = true;
-
- break;
-
- }
- }
- if(flag)
- {
- System.out.println(line+"已登录!");
-
- bufout.println("ok");
-
- bufout.println("欢迎"+line+"登录!");
-
- break;
- }
- else
- {
- System.out.println(line+"尝试登录!");
-
- bufout.println("no");
复制代码 在处理服务器端的并发登录时,在线程类里边,老师是把BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter bufout = new PrintWriter(s.getOutputStream(),true);
这三个流放在了循环里面,我如果把这三个流放在循环外边有什么不同吗?
在处理客户端登录时(最多登录三次),这三个流是放在循环外边的。
|