Client server synchronization problem
I have a client and server connected with ObjectOutputStream to ObjectInputStream pairs flowing in both directions over a TCP connection. The client writes two objects and tries to read on object from the server. The server reads two objects and then writes an object to the client.
Through debug statements, I've found that the server successfully reads the two objects from the client and completes its writeObject() call back to the client. But the client blocks forever trying to read the object that the server wrote to the stream. It seems simple enough to synchronize these reads and writes, but I'm obviously missing something important. I don't understand why the client can't read the callback.
Here are the relevant sections of the client and server code:
Client code:
out = tcpSock.getOutputStream();
oos =new ObjectOutputStream(out);
oos.writeObject(new String("password"));
oos.writeObject(fileBase);
in = tcpSock.getInputStream();
ois =new ObjectInputStream(in);
connectStatus = (Integer)ois.readObject();//client hangs here
oos.writeObject(bos.toByteArray());[
Server code:
in = tcpConnection.getInputStream();
ois =new ObjectInputStream(in);
password = (String) ois.readObject();
fileName = (String) ois.readObject();
out = tcpConnection.getOutputStream();
oos =new ObjectOutputStream(out);
oos.write(new Integer(0));//last statement executed by server
fileAsByteArray = (byte[])ois.readObject();
# 1
You have this:
connectStatus = (Integer)ois.readObject();//client hangs here
which is written by this:
oos.write(new Integer(0));//last statement executed by server
This is not symmetrical. Change the latter to
oos.writeObject(new Integer(0));
Also it's always best to create the ObjectOutputStream before the ObjectInputStream at both ends.
ejpa at 2007-7-8 23:54:32 >

# 2
I could have stared at the code for another three hours, and still not seen that. Thank You.
I ran into a subsequent problem, and since it still deals with client server synchronization and the code segment we're discussing, I'm going to post it in this thread as well.
Perhaps I'm abusing output and input streams, but here's what I'm trying to do now. I was reading and writing objects exclusively, but now I've re-factored the code and would like to interleave sending objects via the ObjectOutputStream / ObjectInputStream with streaming bytes directly via the underlying OutptStream / InputStream pair. I can transition from sending objects to streaming bytes, but I run into a problem when I try to go back to sending an object. I stream the bytes from the client to the server, and I have to close the OutputStream when I'm done, because it's an opaque byte stream and that's the only way I can signal the server that the file transfer is done.
But then I find that when I try to write from the server to the client, the socket has been closed. Apparently when the client closes it's OutputStream, it closes the socket, even though the client's InputStream and the Server's OutputStream were not closed. My revised code is below.
client code:
fis = new FileInputStream(fileName);
out = tcpSock.getOutputStream();
oos = new ObjectOutputStream(out);
oos.writeObject(new String("password"));
oos.writeObject(fileBase);
in = tcpSock.getInputStream();
ois = new ObjectInputStream(in);
connectStatus = (Integer)ois.readObject();
int i = 0;
byte[] buf = new byte[1024];
while((i=fis.read(buf))!=-1) {
out.write(buf, 0, i);
}
fis.close();
oos.close();
status = (Integer)ois.readObject();//SocketException occurs here. Message: Socket is closed
server code:
out = tcpConnection.getOutputStream();
in = tcpConnection.getInputStream();
oos = new ObjectOutputStream(out);
ois = new ObjectInputStream(in);
password = (String) ois.readObject();
fileName = (String) ois.readObject();
oos.writeObject(new Integer(0));
fos = new FileOutputStream(path + File.separator + fileName);
while((i=in.read(buf))!=-1) {
fos.write(buf, 0, i);
}
oos.writeObject(status);//Socket Exception occurs here. Message: Software caused connection abort: socket write error