0

I'm trying to make a Client-Server application where the Server retrieves a list of emails (identified as .txt files) and send all their content through an ArrayList of Email (this is an Object). Now, I get an error on the client, more specifically on the line where I read the object. This is my server:

            File dir = new File(nomeAccount);
            String[] tmp = new String[5];
            ArrayList<Email> arr = new ArrayList<Email>();
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            int i = 0;
            for (File file : dir.listFiles()) {
                if (file.isFile()) {
                    Scanner input = new Scanner(System.in);
                    input = new Scanner(file);
                    while (input.hasNextLine()) {
                        tmp[i++] = input.nextLine();
                    }
                    input.close();
                }
                Date data = df.parse(tmp[4]);
                arr.add(new Email((Integer.parseInt(tmp[0])), tmp[1], nomeAccount, tmp[2], tmp[3], data));
                i = 0;
            }

            //PHASE 3: The server sends the ArrayList to the client
            try {
                ObjectOutputStream objectOutput = new ObjectOutputStream(incoming.getOutputStream());
                objectOutput.writeObject(arr);
            } catch (IOException e) {
                e.printStackTrace();
            }

This is the client:

//PHASE 2: The client receives the ArrayList with the emails
        ObjectInputStream inStream = new ObjectInputStream(s.getInputStream());
        email = (ArrayList<Email>) inStream.readObject();
        inStream.close();

I get the error here: ObjectInputStream inStream = new ObjectInputStream(s.getInputStream());
This is the error:

    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Socket is closed
    at java.net.Socket.getInputStream(Socket.java:903)
    at mailbox.DataModel.loadData(DataModel.java:73)
    at mailbox.ListController.initModel(ListController.java:28)
    at mailbox.MailBox.start(MailBox.java:37)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application mailbox.MailBox
JimBelushi2
  • 285
  • 1
  • 3
  • 18
  • 1
    Once you closed the `inStream`, it stays closed. You can't re-open it. You have to design your protocol such that you will not close the stream until you exit the program. – RealSkeptic Dec 02 '18 at 11:06
  • I would like to close it as soon as I receive the ArrayList, as I wrote there. the error is two line before closing it – JimBelushi2 Dec 02 '18 at 11:09
  • The error you receive is probably due to you having closed it in a previous use of the same socket. Once you close it you can no longer use `s.getInputStream()` with the same socket. As I said, you have to design your program so that it doesn't close the stream until you have finished using the socket. – RealSkeptic Dec 02 '18 at 11:36
  • This is what I have before: `PrintWriter out = new PrintWriter(s.getOutputStream(), true); out.print(account + "\r\n"); // send to server out.flush(); out.close();` If I comment the out.close() it gives me a build error – JimBelushi2 Dec 02 '18 at 11:43
  • Quoting from the documentation of `s.getOutputStream`: _Closing the returned OutputStream will close the associated socket._ Please read the documentation carefully and **do not close any streams until you're completely done with the socket**. – RealSkeptic Dec 02 '18 at 11:45

0 Answers0