Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.io.*;
- import java.net.*;
- import static java.lang.System.out;
- public class Server
- {
- static Map<String, Integer> files = new HashMap<String, Integer>(); //contains file names and the access level required to open them
- static Map<String, Integer> users = new HashMap<String, Integer>();
- public static void main(String[] args)
- {
- out.println("Starting server...");
- File file = new File("datafile.txt");
- try
- {
- //read datafile.txt into files hashmap
- out.println("Reading datafile.txt");
- Scanner scannerdata = new Scanner(file);
- while (scannerdata.hasNextLine())
- {
- String line = scannerdata.nextLine();
- line.trim();
- String field[] = line.split(":");
- files.put(field[0], Integer.parseInt(field[1]));
- }
- scannerdata.close();
- }
- catch (FileNotFoundException e)
- {
- out.println("datafile.txt not found!");
- }
- ////////debug////////////
- //for (Entry<String, Integer> entry : files.entrySet())
- //{
- // String key = entry.getKey();
- // Integer value = entry.getValue();
- // out.println(key);
- // out.println(value);
- //}
- //int debugclearance = files.get("nuclearlaunchcodes.txt");
- //out.println(debugclearance);
- ////////debug////////////
- //read userfile.txt into users hashmap
- file = new File("userfile.txt");
- try
- {
- out.println("Reading userfile.txt");
- Scanner scannerusers = new Scanner(file);
- while (scannerusers.hasNextLine())
- {
- String line = scannerusers.nextLine();
- line.trim();
- String field[] = line.split(":");
- users.put(field[0], Integer.parseInt(field[1]));
- }
- scannerusers.close();
- }
- catch (FileNotFoundException e)
- {
- out.println("userfile.txt not found!");
- }
- ////////debug////////////
- //for (Entry<String, Integer> entry : users.entrySet())
- //{
- // String key = entry.getKey();
- // Integer value = entry.getValue();
- // out.println(key);
- // out.println(value);
- //}
- //int debugclearance = users.get("paul");
- //out.println(debugclearance);
- ////////debug////////////
- //be careful: new code ahead!//
- String clientinput;
- boolean check = false;
- int accesslevel = 0;
- while(true)
- {
- try
- {
- ServerSocket welcomeSocket = new ServerSocket(19000);
- Socket connectionSocket = welcomeSocket.accept();
- BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
- DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
- while(!check)
- {
- outToClient.writeBytes("What is your username?");
- outToClient.writeBytes(" ");
- clientinput = inFromClient.readLine();
- String username = clientinput;
- if(users.get(username) == null)
- {
- outToClient.writeBytes("Invalid username");
- }
- else
- {
- check = true;
- }
- //try
- //{
- // accesslevel = users.get(username);
- // check = true;
- //}
- //catch(NullPointerException e)
- //{
- // outToClient.writeBytes("Incorrect username");
- // outToClient.writeBytes(" ");
- //}
- }
- check = false;
- //debug
- outToClient.writeBytes("It worked!");
- // while(!check)
- // {
- // try
- // {
- // outToClient.writeBytes("");
- // }
- // catch(NullPointerException e)
- // {
- //
- // }
- // }
- //outToClient.writeBytes(clientinput);
- }
- catch(IOException e)
- {
- //out.println("Connection to client lost");
- }
- }
- ///new code
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement