2015-11-12 36 views
1

我正在创建一个客户端和服务器设置,以便发送服务器命令并接收回复。但是,当我运行它的客户端抛出和EOFException。我知道这是文件异常的结束,但我不确定我做错了什么,或者我可以如何解决它。ObjectInputStream在客户端 - 服务器设置上抛出EOFException

代码服务器:

public class Server { 

private ServerSocket server; 
private Socket connection; 
private ObjectOutputStream output; 
private ObjectInputStream input; 

//SET UP AND RUN SERVER 
public void startRunning() { 
    try { 
     server = new ServerSocket(6789, 100); 
     while (true) { 
      try { 
       waitForConnection(); 
       setupStreams(); 
       whileRunning(); 
      } catch (Exception e) { 

      } finally { 
       closeAll(); 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

//DURING RUN 
private void whileRunning() throws IOException { 
    String message = "You are now connected"; 
    sendMessage(message); 
    System.out.println("Connected to client"); 

} 

//WAIT FOR CONNECTION THEN DISPLAYS INFO 
private void waitForConnection() throws IOException { 
    System.out.println("Waiting for connection....."); 
    connection = server.accept(); 
    System.out.println("Now connected to " + connection.getInetAddress().getHostAddress()); 
} 

//SETS UP STREAMS 
private void setupStreams() throws IOException { 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
} 

//SEND MESSAGE TO CLIENT 
private void sendMessage(String message) { 
    try { 
     output.writeObject("SERVER - " + message); 
     output.flush(); 
     System.out.println("SERVER - " + message); 
    } 
    catch (Exception e) { 
     System.out.println("ERROR: Can't send message"); 
    } 
} 

//CLOSE STREAMS AND SOCKETS 
private void closeAll() { 
    System.out.println("Closing Connections"); 
    try { 
     output.close(); 
     input.close(); 
     connection.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

代码启动服务器:

public class RunServer { 

public static void main(String[] args) { 
    Server server = new Server(); 
    server.startRunning(); 
} 

} 

代码客户端:

public class Client { 

private ObjectOutputStream output; 
private ObjectInputStream input; 
private String message = ""; 
private String serverIP; 
private Socket connection; 

public Client(String host) { 
    serverIP = host; 
} 

public void startRunning() { 
    try { 
     connectToServer(); 
     setupStreams(); 
     whileRunning(); 
    } 
    catch (Exception e) { 

    } finally { 
     closeAll(); 
    } 
} 

// CONNECT TO SERVER 
public void connectToServer() throws IOException { 
    System.out.println("Attempted connection..."); 
    connection = new Socket(InetAddress.getByName(serverIP), 6789); 
    System.out.println("Connected to: " + connection.getInetAddress().getHostName());  
} 

// SET UP STREAMS 
private void setupStreams() throws IOException {   output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
    System.out.println("\nStreams Connected"); 
} 

// WHILE CHATTING 
private void whileRunning() throws IOException { 
    do { 
     try { 
      message = (String) input.readObject(); 
      System.out.println(message); 
     } catch (Exception e) { 
      e.printStackTrace(); //In to view exception 
      System.out.println("Unable to get message"); 
      System.exit(0); //In to stop it looping forever (Known issue) 
     } 
    } while (!message.equals("SERVER - END")); 
} 

// CLOSE STREAMS AND SOCKETS 
public void closeAll() { 
    System.out.println("Closing sockets, closing streams"); 
    try { 
     output.close(); 
     input.close(); 
     connection.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 

代码启动客户端:

public class RunClient { 

public static void main(String[] args) { 
    Client client = new Client("localhost"); 
    client.startRunning(); 
} 

} 

当我运行客户端时,它不断地循环说“无法获取消息”永远。 然而,当我查看异常和退出代码(如加入)我得到这个问题:

Attempted connection... 
Connected to: localhost 

Streams Connected 
SERVER - You are now connected 
java.io.EOFException 
    Unable to get message 
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) 
    at java.io.ObjectInputStream.readObject0(Unknown Source) 
    at java.io.ObjectInputStream.readObject(Unknown Source) 
    at main.Client.whileRunning(Client.java:53) 
    at main.Client.startRunning(Client.java:26) 
    at main.RunClient.main(RunClient.java:7) 

任何帮助或建议,将不胜感激。谢谢:)

+0

很有可能是一个异常被抛出,你在这里发现它:'catch(Exception e){0} { }'然后关闭'finally'块中的连接。千万不要压制这样的例外;通常总是做错事。至少你想在某个地方记录它,所以你不知道发生了什么。如果你实际上不能处理这个异常,不要抓住它。 – dsh

+0

是的,我想赶上它,以避免它崩溃的程序,我只是不知道如何处理它,并实际上从服务器正确接收数据 – AKTheKnight

回答

1

EOFException当到达流的末尾时会引发,当对等关闭连接时发生。

您需要单独捕捉它,而不是将其视为错误。

我还建议你将System.exit(0);更改为break;

+0

啊好吧,你会怎么建议我去对待它,所以我是能够在对等关闭连接时停止尝试读取消息?我对如何做到这一点感到困惑。顺便谢谢你的回答 – AKTheKnight

+1

抓住异常并休息。 – EJP

相关问题