Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

EOFException through by ObjectInputStream.readObject()

843790May 23 2007 — edited May 26 2007
I have a client-server application and I send messages between them by using a BufferedReader and PrintWriter to send the text and ObjectInputStream and ObjectOutputStream to send an enum type representing the type of message. Both the client and the server make threads that listen for incoming messages and handle them appropriately. I assume that both ObjectInputStream.readObject() and BufferedReader.readLine() block until there is something actually in the stream, otherwise my approach is probably wrong. Sometimes, but not always, when the client connects to the server, the ObjectInputStream on the server throws an EOFException and I'm not sure why this is happening. Some other thread I read suggested including ObjectOutputStream.flush() on the client side, but this didn't seem to fix the problem. I'm not sure if I should still inlucde flush() and where (for ObjectOutputStream only or for ObjectInputStream as well?). The relevant code snippets are shown below:

Server code:
private void processMessage(MessageType type, String input) {
    	String user, pass;

    	try {
    	
    	switch(type) {
    	case Create:
    		user = input.substring(0, input.indexOf(" "));
    		input = input.substring(input.indexOf(" ")+1);
    		pass = input;
    		
    		client.getObjectOut().writeObject(MessageType.Create);
    		
    		if(LostHavenServer.registered.containsKey(user))
    			client.getWriter().println("Username already exists");
    		else {
	    		LostHavenServer.registered.put(user, new Player(user, pass));
	    		client.getWriter().println("Account created successfully");
    		}
    		
    		closeConnection();
    		break;
    	case Login:
    		user = input.substring(0, input.indexOf(" "));
    		input = input.substring(input.indexOf(" ")+1);
    		pass = input;
    		
    		client.getObjectOut().writeObject(MessageType.Login);

    		if(!LostHavenServer.registered.containsKey(user))
    			client.getWriter().println("No such player exists");
    		else if(!LostHavenServer.registered.get(user).getPass().equals(pass))
    			client.getWriter().println("Incorrect password");
    		else if(LostHavenServer.loggedOn.containsKey(user))
    			client.getWriter().println("This player is already logged on");
    		else {
	    		client.getWriter().println("Login successful");
    		}
    	}
    	
    	} catch(IOException ioe) {
			ioe.printStackTrace();
		}
    }
    
    public void run() {
    	try {
	    	while(true) {
	    		processMessage((MessageType)objIn.readObject(), in.readLine());
	    	}
		}catch(ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}catch(SocketException se) {
			if(!terminated)
				System.out.println("the client disconnected");
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
		
	} 
Client code:
public void sendMessage(MessageType type, String input) {
    	System.out.println("message started");
    	try {
    		objOut.writeObject(type);
    		out.println(input);
    	} catch(IOException ioe) {
    		ioe.printStackTrace();
    	}
    	System.out.println("message ended");
    }
    
    public void run() {
    	try {
    		while(connected) {
				main.processMessage((MessageType)objIn.readObject(), in.readLine());
    		}
		} catch(ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		} catch(SocketException se) {
			if(!terminated) {
				System.out.println("the server disconnected");
			}
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2007
Added on May 23 2007
5 comments
346 views