The Java server does not close properly?

advertisements

I've tried everything, and I'm at wit's end. I'm writing a simple file server that will let the user telnet in, supply a username and search for the access level given to that user, and then show a list of files and only allow them to see the contents of the file if they have a sufficient access level. This code is for the server side, I'm just using PuTTY for the client.

The first run works as expected,

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"

Then I close PuTTY and try a second time, and get this

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

When it should start back from the beginning and ask me for my username. The third attempt, it shows a blank line and disconnects after a few seconds.

The file containing all of the filenames and their access levels looks like this:

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

I think it may be somewhere in here

//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();

But here is my code, in its entirety. I left in a bunch of attempts in vain to fix it by closing everything.

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();
    }

}
}


This is because you keep using the same socket. Also I think that you would like to support multiple connections with multi threading. Maybe you can look at this answer?

Creating a socket server which allows multiple connections via threads and Java

I think that it will put you in the right direction.