I'm getting the following exception for the code included below that. This works fine when the while() loop is excluded. Why is this?
Oct 6, 2011 1:19:31 AM com.mytunes.server.ServerHandler run
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.mytunes.server.ServerHandler.run(ServerHandler.java:68)
Class ServerHandler:
public class ServerHandler extends Thread {
.
.
.
public ServerHandler(...){
...
}
public void run(){
try {
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
is = s.getInputStre开发者_StackOverflow中文版am();
ois = new ObjectInputStream(is);
while(true){
msg = (Messenger) ois.readObject();
String methodType = msg.getKey();
//validating various data passed from the serialized object
if(methodType.equals("validateCard")){
} else if(methodType.equals("validatePIN")){
}
}
} catch (SQLException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally{
try {
ois.close();
is.close();
oos.close();
os.close();
s.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Class Server
public class Server{
...
ServerSocket ss;
...
public static void main(String args[]){
Server server = new Server();
server.init();
}
public void init(){
try {
System.out.println("Server started...");
ss = new ServerSocket(port);
System.out.println("Listening on " + ss.getInetAddress() + ":" + ss.getLocalPort());
System.out.println("Waiting for clients...");
while(true){
Socket incoming_socket = ss.accept(); // returns a Socket connection object if received
new ServerHandler(...).start();
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You keep trying to read objects forever, never breaking out of the loop. When the client closes the connection, the stream will run out of data, and the ObjectInputStream.readObject
method will throw the exception you're seeing.
How many objects did you expect to be in the stream, and why are you reading past the end of them?
精彩评论