Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Map.Entry;
  12. import java.util.Scanner;
  13. import java.io.*;
  14. import java.net.*;
  15.  
  16. import static java.lang.System.out;
  17.  
  18. public class Server
  19. {
  20.     static Map<String, Integer> files = new HashMap<String, Integer>(); //contains file names and the access level required to open them
  21.     static Map<String, Integer> users = new HashMap<String, Integer>();
  22.    
  23.     public static void main(String[] args)
  24.     {
  25.         out.println("Starting server...");
  26.        
  27.         File file = new File("datafile.txt");
  28.         try
  29.         {
  30.             //read datafile.txt into files hashmap
  31.             out.println("Reading datafile.txt");
  32.             Scanner scannerdata = new Scanner(file);
  33.  
  34.             while (scannerdata.hasNextLine())
  35.             {
  36.                 String line = scannerdata.nextLine();
  37.                 line.trim();
  38.                 String field[] = line.split(":");
  39.                
  40.                 files.put(field[0], Integer.parseInt(field[1]));
  41.             }
  42.             scannerdata.close();
  43.         }
  44.         catch (FileNotFoundException e)
  45.         {
  46.             out.println("datafile.txt not found!");
  47.         }
  48.         ////////debug////////////
  49.         //for (Entry<String, Integer> entry : files.entrySet())
  50.         //{
  51.         //  String key = entry.getKey();
  52.         //  Integer value = entry.getValue();
  53.          
  54.         //  out.println(key);
  55.         //  out.println(value);
  56.         //}
  57.         //int debugclearance = files.get("nuclearlaunchcodes.txt");
  58.         //out.println(debugclearance);
  59.         ////////debug////////////
  60.        
  61.         //read userfile.txt into users hashmap
  62.         file = new File("userfile.txt");
  63.         try
  64.         {
  65.             out.println("Reading userfile.txt");
  66.             Scanner scannerusers = new Scanner(file);
  67.  
  68.             while (scannerusers.hasNextLine())
  69.             {
  70.                 String line = scannerusers.nextLine();
  71.                 line.trim();
  72.                 String field[] = line.split(":");
  73.                
  74.                 users.put(field[0], Integer.parseInt(field[1]));
  75.             }
  76.             scannerusers.close();
  77.         }
  78.         catch (FileNotFoundException e)
  79.         {
  80.             out.println("userfile.txt not found!");
  81.         }
  82.         ////////debug////////////
  83.         //for (Entry<String, Integer> entry : users.entrySet())
  84.         //{
  85.         //  String key = entry.getKey();
  86.         //  Integer value = entry.getValue();
  87.          
  88.         //  out.println(key);
  89.         //  out.println(value);
  90.         //}
  91.         //int debugclearance = users.get("paul");
  92.         //out.println(debugclearance);
  93.         ////////debug////////////
  94.        
  95.         //be careful: new code ahead!//
  96.        
  97.         String clientinput;
  98.         boolean check = false;
  99.         int accesslevel = 0;
  100.        
  101.        
  102.         while(true)
  103.         {
  104.             try
  105.             {
  106.                 ServerSocket welcomeSocket = new ServerSocket(19000);
  107.                 Socket connectionSocket = welcomeSocket.accept();
  108.                 BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  109.                 DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  110.                
  111.                 while(!check)
  112.                 {
  113.                     outToClient.writeBytes("What is your username?");
  114.                     outToClient.writeBytes(" ");
  115.                     clientinput = inFromClient.readLine();
  116.                     String username = clientinput;
  117.                    
  118.                    
  119.                     if(users.get(username) == null)
  120.                     {
  121.                         outToClient.writeBytes("Invalid username");
  122.                     }
  123.                     else
  124.                     {
  125.                         check = true;
  126.                     }
  127.                    
  128.                     //try
  129.                     //{
  130.                     //    accesslevel = users.get(username);
  131.                     //    check = true;
  132.                     //}
  133.                     //catch(NullPointerException e)
  134.                     //{
  135.                     //  outToClient.writeBytes("Incorrect username");
  136.                     //    outToClient.writeBytes("  ");
  137.                     //}
  138.                 }
  139.                
  140.                 check = false;
  141.                
  142.                 //debug
  143.                 outToClient.writeBytes("It worked!");
  144.                
  145. //              while(!check)
  146. //              {
  147. //                  try
  148. //                  {
  149. //                      outToClient.writeBytes("");
  150. //                  }
  151. //                  catch(NullPointerException e)
  152. //                  {
  153. //                     
  154. //                  }
  155. //              }
  156.                
  157.                
  158.                 //outToClient.writeBytes(clientinput);
  159.                
  160.             }
  161.             catch(IOException e)
  162.             {
  163.                 //out.println("Connection to client lost");
  164.             }
  165.            
  166.        
  167.         }
  168.         ///new code
  169.        
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement