Arvidus
Arvidus

Reputation: 21

Sockets - No continuous response

I'm delving into sockets for the first time. The point of the project is for the client to be able to get access to a contact list (CSV) in the server by writing "getall" and exit the program through just that command ("Exit").

The problem is that the client can only write the command and receive the list once and then the server doesn't respond to the client's input anymore.

Here is the socket code for the server and client respectively:

Server:

public class CatalogueServer extends CatalogueLoader {

ServerSocket serverSocket;
ArrayList<CatalogueEntry> catalogue;

public void startServer(int port, String catalogueFile) {

    catalogue = loadLocalCatalogue(catalogueFile);

    try {
        serverSocket = new ServerSocket(port);

        while (true) {
            Socket clientSocket = serverSocket.accept();

            new Thread(
                    new Runnable() {
                        public void run() {
                            try {

                                InputStream inputStream = clientSocket.getInputStream();
                                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");

                                BufferedReader BR = new BufferedReader(inputStreamReader);

                                OutputStream outputStream = clientSocket.getOutputStream();
                                PrintWriter PW = new PrintWriter(outputStream);
                                String clientInput;

                                while ((clientInput = BR.readLine()) != null) {
                                    System.out.println(clientInput);
                                    if (clientInput.equals("getall")) {
                                        System.out.println(printCatalogue(catalogue));
                                        PW.println(printCatalogue(catalogue));
                                        PW.flush();
                                        break;
                                    } else if (clientInput.equals("exit")) {
                                        clientSocket.close();
                                        BR.close();
                                        PW.close();
                                        break;
                                    } else {
                                        PW.flush();
                                        break;
                                    }
                                }
                                PW.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
            ).start();
        }
    } catch (Exception i) {
        i.printStackTrace();
    }
  }
}

Client:

public class TestClient {

public static void main(String[] args) {

    try {
        Socket socket = new Socket("localhost", 5253);
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

        BufferedReader BR = new BufferedReader(inputStreamReader);

        while (true) {

            String clientInput;
            String serverFeedback;

            PrintWriter PW = new PrintWriter(outputStream);

            Scanner inputScan = new Scanner(System.in, "UTF-8");
            clientInput = inputScan.nextLine();

            PW.println(clientInput);
            PW.flush();

            while ((serverFeedback = BR.readLine()) != null)  {
                System.out.println(serverFeedback);
            }

            if (clientInput.equals("exit")) {
                PW.close();
                socket.close();
                break;
            }
            PW.close();
        }
    }
    catch (IOException e){
        e.printStackTrace();
    }
  }
}

I have tried alternating the position and renewal of the readers and writers. But I'm uncertain of where exactly the problem starts.

Upvotes: 0

Views: 31

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

When you do

        while ((serverFeedback = BR.readLine()) != null)  {
            System.out.println(serverFeedback);
        }

You are reading until you reach the end of the stream, i.e. until there is nothing left. As such there is nothing after this.

If you want to reuse the connection, you have to write the code which doesn't use this pattern and only reads until it should stop reading.

Upvotes: 2

Related Questions