EOFException while using ObjectInputStream

I am writing a Server/Client Application using Sockets and ObjectStreams. If I run the Writer and Readers Loops in Threads I get an EOFException in my Server, after one run of the loop. Executing the Writer in the main thread prevents this, but that way I cant use a GUI.

How to solve this Exception?

Client:

try (Socket socket = new Socket("127.0.0.1", 1234)) { beginReadCommands(socket); beginWriteCommands(socket); View w = new View(); } private static void beginWriteCommands(Socket socket) { Thread thread; thread = new Thread(() -> writeCommands(socket)); thread.start(); } private static void writeCommands(Socket socket) { try { ObjectOutputStream out = new ObjectOutputStream( socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); String line = ""; while (line != null) { if (line.equals("!q")) { line = null; } else { Command msg = new Command("GETFILES", ""); out.writeObject(msg); // waiting line = in.readLine(); } } out.writeObject(new Command("EXIT", "")); out.writeObject(null); } catch (IOException e) { } }

Server:

public void startServer() throws IOException, ClassNotFoundException { // create the socket server object server = new ServerSocket(port); boolean running = true; while (running) { System.out.println("Waiting for client request"); Socket socket = server.accept(); ObjectInputStream ois = new ObjectInputStream( socket.getInputStream()); Command message = (Command) ois.readObject(); System.out.println("Message Received: " + message); ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream()); switch (message.getCommand()) { case "": break; case "GETFILES": { oos.writeObject(new Command("SENDFILES", "")); State s=new State("/"); s.listFilesForFolder(); s.getFiles(); oos.writeObject(s); break; } case "EXIT": { running=true; break; } } } server.close(); }

Category:java Views:6 Time:2018-05-15

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.232 (s). 11 q(s)