网络编程(TCP-客户端并发登录)

  1 package netTest;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.FileReader;
  5 import java.io.IOException;
  6 import java.io.InputStreamReader;
  7 import java.io.PrintWriter;
  8 import java.net.ServerSocket;
  9 import java.net.Socket;
 10 
 11 public class LoginServer {
 12 
 13     public static void main(String[] args) throws Exception {
 14         // TODO Auto-generated method stub
 15         ServerSocket ss = new ServerSocket(10003);
 16         // 或取多个客户端需要用到线程
 17         while(true){
 18             Socket s = ss.accept();
 19             new Thread(new  UserThread(s)).start();
 20         }
 21     }
 22 
 23 }
 24  class UserThread implements Runnable {
 25     private Socket s;
 26     UserThread(Socket s){
 27         this.s = s;
 28     }
 29     @Override
 30     public void run() {
 31         // TODO Auto-generated method stub
 32         try{
 33             for(int x = 0;x<3;x++){
 34                 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
 35                 String name = bufIn.readLine();
 36                 if(name == null){
 37                     break;
 38                 }
 39                 BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
 40                 PrintWriter out = new PrintWriter(s.getOutputStream(),true);
 41                 String line = null;
 42                 boolean flag = false;
 43                 while((line = bufr.readLine())!=null){
 44                     if(line.equals(name)){
 45                         flag = true;
 46                         break;
 47                     }
 48                 }
 49                 if(flag){
 50                     System.out.println(name+",已登录");
 51                     out.println(name+"欢迎光临");
 52                     break;
 53                 }
 54                 else{
 55                     System.out.println(name+",尝试登录");
 56                     out.println(name+"用户名不存在。");
 57                 }
 58             }
 59             s.close();
 60         }
 61         catch(Exception e){
 62             e.printStackTrace();
 63         }
 64     }
 65 
 66 }
 67 
 68 package netTest;
 69 
 70 import java.io.BufferedReader;
 71 import java.io.IOException;
 72 import java.io.InputStreamReader;
 73 import java.io.PrintWriter;
 74 import java.net.InetAddress;
 75 import java.net.Socket;
 76 import java.net.UnknownHostException;
 77 
 78 /*
 79  * 只能校验三次。
 80  * 
 81  */
 82 public class LoginClient {
 83     public static void main(String[] args) throws Exception, Exception {
 84         // TODO Auto-generated method stub
 85         //1.建立端点
 86         Socket s = new Socket(InetAddress.getLocalHost(), 10003);
 87         //2.组织数据(先写输入输出流 )
 88         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
 89         PrintWriter out = new PrintWriter(s.getOutputStream(),true);
 90         BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
 91         //只能发送三次
 92         for(int x = 0;x<3;x++){
 93             String line = bufr.readLine();
 94             if(line == null){
 95                 break;
 96             }
 97             
 98             out.println(line);
 99             String info = bufIn.readLine();
100             System.out.println("info:"+info);
101             if(info.contains("欢迎")){
102                 break;
103             }
104             
105         }
106         bufr.close();
107         s.close();
108     }
109 }

 

posted @ 2016-01-21 20:40  HuberyQian  阅读(495)  评论(0编辑  收藏  举报