2014-02-27 50 views
0

我試過了所有的東西,而且我的智慧已經結束了。我正在寫一個簡單的文件服務器,讓用戶telnet,提供一個用戶名並搜索給予該用戶的訪問級別,然後顯示一個文件列表,只允許他們查看文件的內容有足夠的訪問級別。此代碼用於服務器端,我只是爲客戶端使用PuTTY。簡單的服務器不能正常關閉?

第一次運行時正常工作,

What is your username? paul 

Filename:Access Level 
chemistryhomework.txt:5 
messageforbob.txt:0 
nuclearlaunchcodes.txt:10 
Which file would you like to read? 
nuclearlaunchcodes.txt 
The launch password is "UnlimitedBreadsticks" 

然後我關閉膩子,並嘗試第二次,並獲得該

Filename:Access Level 
chemistryhomework.txt:5 
messageforbob.txt:0 
nuclearlaunchcodes.txt:10 
Which file would you like to read? 

當它應該開始從一開始就回來找我我的用戶名。第三次嘗試時,它顯示一條空白行並在幾秒鐘後斷開連接。

包含所有的文件名和他們的訪問級別的文件是這樣的:

chemistryhomework.txt:5 
nuclearlaunchcodes.txt:10 
messageforbob.txt:0 

我認爲這可能是某處這裏

//show list of files 
int fileaccess; 
outToClient.writeBytes("\n\rFilename:Access Level \n \r"); 
for (Entry<String, Integer> entry : files.entrySet()) //show all files and access levels 
{ 
     String key = entry.getKey(); 
     Integer value = entry.getValue(); 
     outToClient.writeBytes(key + ":" + value + "\n \r"); 
} 
while(!check) 
{ 
//ask user for file to read 
outToClient.writeBytes("Which file would you like to read? \n \r"); 
String filetoread = inFromClient.readLine(); 
try 
{ 
fileaccess = files.get(filetoread); //get access level requirement of file 
    if (fileaccess > accesslevel) //if user is lacking the access level 
    { 
     outToClient.writeBytes("Error: You do not have sufficient privileges to access this file! \n \r"); 
     welcomeSocket.close(); 
     connectionSocket.close(); 
    } 
    else 
    { 
     try //try to read the file, if it exists. Just a backup verification 
     { 
      file = new File(filetoread); 
      Scanner scannerfile = new Scanner(file); 
      while (scannerfile.hasNextLine()) 
        { 
         String line = scannerfile.nextLine(); 
         line=line.trim(); 
         outToClient.writeBytes(line + "\n\r"); 
        } 
      outToClient.writeBytes("\n\r"); 
        scannerfile.close(); 
     } 
    catch (FileNotFoundException e) 
    { 
      outToClient.writeBytes("File not found! \n \r"); 
    } 
    check = true; 
    } 
    } 
    catch (NullPointerException e) //first line of defense for checking made-up files 
{ 
    outToClient.writeBytes("Error: File does not exist! \n \r"); 
} 
} 
check = true; 
//outToClient.writeBytes("Press enter to terminate session"); //user can see contents of file just in case disconnecting 
//inFromClient.readLine();         //closes the window (like PuTTY) 
inFromClient.close(); 
outToClient.close(); 
welcomeSocket.close(); 
connectionSocket.close(); 

但這裏是我的代碼,將其全部。我徒勞地試圖通過關閉所有東西來解決問題。

package server; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
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>(); //contains user names and the access level assigned to them 

public static void main(String[] args) throws IOException 
{ 
    out.println("Starting server..."); 

    //read datafile.txt into files hashmap 
    File file = new File("datafile.txt"); 
    try 
    { 
     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//////////// 

    String clientinput; 
    boolean check = false; 
    int accesslevel = 0;   

    while(true) 
    { 
     ServerSocket welcomeSocket = null; 
     Socket connectionSocket = null; 
     BufferedReader inFromClient = null; 
     DataOutputStream outToClient = null; 

     try 
     { 
      //start connection 
      //ServerSocket welcomeSocket = new ServerSocket(19000); 
      //Socket connectionSocket = welcomeSocket.accept(); 

      welcomeSocket = new ServerSocket(19000); 
      connectionSocket = welcomeSocket.accept(); 

      //BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
      //DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 

      inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
      outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 


      while(!check) //authenticate user 
      { 
       outToClient.writeBytes("What is your username?"); 
       outToClient.writeBytes(" "); 
       String username = inFromClient.readLine(); 

       //alternate method I tried 
       //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 \n \r"); 
        welcomeSocket.close(); 
        connectionSocket.close(); 
       } 
      } 

      check = false; 

      //debug 
      //outToClient.writeBytes("It worked!"); 
      //outToClient.writeBytes(clientinput); 

      //show list of files 
      int fileaccess; 
      outToClient.writeBytes("\n\rFilename:Access Level \n \r"); 
      for (Entry<String, Integer> entry : files.entrySet()) //show all files and access levels 
      { 
       String key = entry.getKey(); 
       Integer value = entry.getValue(); 
       outToClient.writeBytes(key + ":" + value + "\n \r"); 
      } 
      while(!check) 
      { 
       //ask user for file to read 
       outToClient.writeBytes("Which file would you like to read? \n \r"); 
       String filetoread = inFromClient.readLine(); 
       try 
       { 
        fileaccess = files.get(filetoread); //get access level requirement of file 
        if (fileaccess > accesslevel) //if user is lacking the access level 
        { 
         outToClient.writeBytes("Error: You do not have sufficient privileges to access this file! \n \r"); 
         welcomeSocket.close(); 
         connectionSocket.close(); 
        } 
        else 
        { 
         try //try to read the file, if it exists. Just a backup verification 
         { 
          file = new File(filetoread); 
          Scanner scannerfile = new Scanner(file); 
          while (scannerfile.hasNextLine()) 
          { 
           String line = scannerfile.nextLine(); 
           line=line.trim(); 
           outToClient.writeBytes(line + "\n\r"); 
          } 
          outToClient.writeBytes("\n\r"); 
          scannerfile.close(); 
         } 
         catch (FileNotFoundException e) 
         { 
          outToClient.writeBytes("File not found! \n \r"); 
         } 
         check = true; 
        } 
       } 
       catch (NullPointerException e) //first line of defense for checking made-up files 
       { 
        outToClient.writeBytes("Error: File does not exist! \n \r"); 
       } 
      } 
      check = true; 
      //outToClient.writeBytes("Press enter to terminate session"); //user can see contents of file just in case disconnecting 
      //inFromClient.readLine();         //closes the window (like PuTTY) 
      inFromClient.close(); 
      outToClient.close(); 
      welcomeSocket.close(); 
      connectionSocket.close(); 
     } 
     catch(IOException e) 
     { 
      //out.println("Connection to client lost"); 
      inFromClient.close(); 
      outToClient.close(); 
      welcomeSocket.close(); 
      connectionSocket.close(); 
     } 
     inFromClient.close(); 
     outToClient.close(); 
     welcomeSocket.close(); 
     connectionSocket.close(); 
    } 

} 
} 
+2

你有沒有使用一個你應該使用的地方?你是否試過[調試你的代碼?](https://www.google.com.au/search?q =如何+調試+ Java和OQ =如何+調試+ java的) – gerrytan

回答

1

你的行結束符遍佈各處。你正在使用\n \r,\n\r,各種各樣的東西。這些都不是有效的行結束符。僅使用\n\r\n。並且在某些地方,如果接收機正在使用readLine().