EOFException with Server

I抦 a currently creating a client server program at work that passes a several different types of MessageObjects back and forth. All of my different messages extend MessageObjects and implement serializable. The problem I am having is that when I try to pass certain messages from the server to the client I am receiving an EOFException from the server, shortly followed by a Null exception on the client side. Oddly enough certain messages work fine and can be passed back and forth with no problem.

Here is the simplified code:

privateboolean sendMessage(MessageObject msg){

try{

if (this.isConnected){

msg.whoOwns =null;

System.out.println("Server Sending: " + msg.toString());

outObj.flush();

outObj.writeObject(msg);

if(msg.getMessageType() == Globals.DISCONNECT){

this.disconnect();

}

returntrue;

}

}catch (IOException e){

System.err.println("Error:" + e);

this.disconnect();

returnfalse;

}}

Using Debug mode I know that the msg being passed has been created and has all of its fields set

Here is the basic code for the MessageObject class that everything extends:

publicabstractclass MessageObjectimplements Serializable{

public String name ="";

public Object whoOwns;

protectedint messageType;

public MessageObject(){

this.name ="orgin";

}

//May need to be abstract

public JPanel buildGUI(){

returnnull;

}

Note: whoOwns variable is only used to keep track of which thread owns the message so I know which client to send a message back to. I null it before i pass it back so it doesn't need to be serialized

Here is the basic code for one of the messages I am having trouble sending:

publicclass CreateChanResponseextends MessageObject{

privateint createStatus = 0;

privateint channelID = 0;

privateint keyError = 0;

public CreateChanResponse(){

this.name ="CreateChanResponse";

this.messageType = Globals.CREATE_CHAN_RESPONSE;

}

public JPanel buildGUI(){

JPanel chanPanel =new JPanel();

chanPanel.setLayout(new GridBagLayout());

chanPanel.setSize(350,350);

return chanPanel;

}

}

Anyones help would greatly be appreciated

[4285 byte] By [halfpeawa] at [2007-10-1 21:12:42]
# 1
Call flush() *after* you call writeObject(). See if that helps.- Saish
Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Unfortunately I tried doing that and I'm still having the same problem. Maybe if someone could explain how two objects that extend the same thing and are basically the same in code, why one would work just fine and the other would give an EOFException when being passed in to an ObjectOutputStream

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
Try having the extended class also implement Serializable. Also, post the receiving end code snippit where you are calling readObject().- Saish
Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

This is the code that is on the client end:

public void run() {

MessageObject msg = null;

try {

while (true) {

if ((msg= (MessageObject)inObj.readObject()) == null)

break;

client.receiveMessage(msg);

if (msg.getMessageType() == Globals.DISCONNECT)

break;

}

} catch (Exception e) {

this.disconnectFromServer();

System.err.println("6Error:" + e);

}

this.disconnectFromServer();

}

If you couldn't tell the Client spawns a thread in order to receive data from the server

This is the error I am getting on the server end

Server Sending: CreateChanResponse

Error:java.io.EOFException

This is the Error I am getting on the client end

Sending:CreateChan

Receive: CreateChanResponse

Error:java.lang.NullPointerException

When I Implement Serializable with my particular message I still get the same error.

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
And you are not using buffered streams either on the receiving or the sending end (e.g., you wrapped a socket input and output stream directly by the respective object input and output stream)? And you have moved flush() to after writeObject() not before?- Saish
Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

Yeah I moved the flush like you suggested and this is how i have my streams set up for my client

this.client = client;

this.socket = new Socket(IPAddress, 1001);

outObj = new ObjectOutputStream(socket.getOutputStream());

inObj = new ObjectInputStream(socket.getInputStream());

client.isConnected = true;

sendMessage(new Connect());

This is how I have my steams set up for my server

this.serverSocket = new ServerSocket(1001);// <-this is actually declared much earlier and passed into my thread object

...

public serverConnection(Socket clientSocket, Server server) { //ThreadObject so I can have multiple connections

try {

this.server = server; //main server

this.clientSocket = clientSocket;

outObj = new ObjectOutputStream(clientSocket.getOutputStream());

inObj = new ObjectInputStream(clientSocket.getInputStream());

}

Hope this clarified what you wanted to know.

Also thank you for helping me with this problem it can be very intimidating coming on to these forums when you are just an entry level Software Engineer, and you are dealing with the much more experienced professionals.

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
Does client.receiveMessage() initiate its own readObject() or another read() method on the socket input stream? Also, are you sure you are getting the EOF at the sending end and not at the receiving end?- Saish
Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8

BTW, you are welcome. And don't worry. If you do a Google and Forum search first, and then post a specific question (which you did admirably), most of the regulars will be glad to help. (I just wish I could have answered your question better). I'm off for the day. Will be back tomorrow in the morning.

- Saish

Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 9

No when the client ReceiveMessage(msg) method is called it receives the MessageObject that should have been read by the clientConnection thread. The only time I do any stream reading is in the run methods in the client and server threads. After the objects are read in they are immediately passed in to the rest of the program.

I am absolutely positive the EOFException is occuring at the sending end of the server. I use eclipse and it keeps my consoles seperate so I can track the progress of both the server and the client. I also went through using the debug mode and I know the error occurs when the server attempts to send the message using the outObj.writeObject(msg) also the program works for most messages just not all of them.

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 10
Are you positive? I just checked the actual source code for both ObjectOutputStream and OutputStream (which it extends). Neither throws an EOFException at any point. There are numerous references in ObjectInputStream. Are you sure?- Saish
Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 11

I did like you suggested and checked the source code for ObjectOutputStream and you are are right it never says it specifically throws an EOFException, it does throw lots of IOExceptions though, and EOFException is a subclass of that. So I'm not sure but I believe that even though it only says it throws IOExceptions it can also throw any subclass of that as well. But I am for certain that the Error is happening on the server end. I just carefully went through each step of the debugger and the error happens write as it tries to output the object.

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 12

I fixed the problem, I have no idea why this fixed the problem but it did. I originally created a public method in my MessageObject class that returned null by default when that method was called, and I defined the actual function within my derived messages classes. When I changed it to return a new instance of a JPanel instead of a null by default, all of my messages worked fine. If you know why this fixed the problem I would love to hear it, otherwise thanks for all your help!

halfpeawa at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 13

Did the call occur inside a constructor? Serialization attempts to invoke the default constructor of a given class when de-serializing (readObject()). If there was a null pointer exception within the constructor, an EOFException could conceivably be thrown. Though, again, I would expect this to occur on the receiving rather than the sending stream.

Just a guess. Glad everything is working.

- Saish

Saisha at 2007-7-13 3:09:34 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...