Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. public void run(){
  2.         String inputLine;
  3.        
  4.         try
  5.         {
  6.             //input stream reader
  7.             //get socket writing and reading streams
  8.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  9.             DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  10.            
  11.             out.writeBytes(simpleprotocol.createMessage("", "Welcome to my Server") + "\n");
  12.  
  13.             //Now start reading input from client
  14.             while((inputLine = in.readLine()) != null) {
  15.                 //reply with the same message, adding some text
  16.                 chooseFunction(inputLine);
  17.                 System.out.println("");
  18.                 System.out.println(inputLine);
  19.                 System.out.println("");
  20.                 System.out.println("");
  21.                 out.writeBytes((inputLine) +"\n");
  22.             }
  23.              
  24.             //client disconnected, so close socket
  25. //            socket.close();
  26.         }
  27.        
  28.         catch (IOException e)
  29.         {
  30.             System.out.println("IOException on socket : " + e);
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.  
  35.    
  36.     public void chooseFunction(String message){
  37.         String[] received = simpleprotocol.decodeMessage(message);
  38.         String function = received[0];
  39.        
  40.         if(function.equals("sign-up")){
  41.             userSignUp(message);
  42.         }else if(function.equals("sign-in")){
  43.             userSignIn(message);
  44.         }else if(function.equals("get-message")){
  45.             getMessage(message);
  46.         }else if(function.equals("send-message")){
  47.             sendMessage(message);
  48.         }
  49.     }
  50.    
  51.    
  52.     public String userSignUp(String credentials){
  53.         String[] received = simpleprotocol.decodeMessage(credentials);
  54.         String username = received[1];
  55.         String password = received[2];
  56.        
  57.        
  58.             if(!(userDetails.containsKey(username))){
  59.                 if(username.length() >=5 && username.length() <=20){
  60.                     if(password.length() >= 8 && password.length() <=32){  
  61.                         userDetails.put(username, password);
  62.                     }
  63.                 }
  64.             }
  65.        
  66.             return simpleprotocol.createMessage("sign-up", "true","User successfully signed up");
  67.            
  68.            
  69. //          try{
  70. //              socket.close();
  71. //          }catch (IOException e){
  72. //              e.printStackTrace();
  73. //          }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement