Advertisement
Guest User

Untitled

a guest
May 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package com.logic;
  5.  
  6. /**
  7.  * @author Mike Rogers
  8.  *
  9.  */
  10. import java.io.*;
  11. import java.net.*;
  12.  
  13. class Server
  14. {
  15.  
  16.     public static void main(String argv[]) throws Exception
  17.     {
  18.           String clientSentence;
  19.           String capitalizedSentence;
  20.          
  21.           String[] authUsers = {"alice", "ted", "bob"};
  22.           String[] authPassW = {"245"};
  23.          
  24.           String user = "";
  25.           String pass = "";
  26.          
  27.           boolean run = true;
  28.           boolean authenticated = false;
  29.           boolean validUser = false;
  30.           boolean validPass = false;
  31.    
  32.            
  33.           System.out.println("Server running...");
  34.          
  35.           while(run == true)
  36.           {
  37.               ServerSocket welcomeSocket = new ServerSocket(6789);
  38.               Socket connectionSocket = welcomeSocket.accept();
  39.               BufferedReader inFromClient = new BufferedReader(new
  40.               InputStreamReader(connectionSocket.getInputStream()));
  41.    
  42.               DataOutputStream  outToClient =
  43.               new DataOutputStream(connectionSocket.getOutputStream());
  44.              
  45.               while(authenticated == false)
  46.               {
  47.                   clientSentence = inFromClient.readLine();
  48.                   outToClient.writeBytes("Please enter a valid username: " + '\n');
  49.                   user = inFromClient.readLine();
  50.                  
  51.                   for(int i = 0; i < authUsers[i].length(); i++)
  52.                       if(user.equalsIgnoreCase(authUsers[i]))
  53.                           validUser = true;
  54.                  
  55.                   outToClient.writeBytes("Please enter a valid password: " + '\n');
  56.                   pass = inFromClient.readLine();
  57.                  
  58.                   for(int i = 0; i < authPassW[i].length(); i++)
  59.                       if(pass.equalsIgnoreCase(authPassW[i]))
  60.                           validPass = true;
  61.                  
  62.                   if(validUser == true && validPass == true)
  63.                   {
  64.                       outToClient.writeBytes("authenticated" + '\n');
  65.                       authenticated = true;
  66.                   }
  67.               }
  68.              
  69.               clientSentence = inFromClient.readLine();
  70.               capitalizedSentence = clientSentence.toUpperCase() + '\n';
  71.               outToClient.writeBytes(capitalizedSentence);
  72.              
  73.               if(capitalizedSentence.equals("QUIT"))
  74.               {
  75.                   run = false;
  76.                   connectionSocket.close();
  77.               }
  78.           }
  79.      }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement