Click to See Complete Forum and Search --> : Write to stream doesnīt work? Strange.


Oceansoul
April 22nd, 2008, 02:34 PM
Itīs me again. I hope noone minds my flood of questions.

This time itīs actually (or should be) rather simple. Only it it is not.

I am still on the client-server program. Here is a part from the server side code:


else if( m_bVidActive ) {
m_oOStream.writeUTF("666:msg");
m_oOStream.writeUTF("Sending video");
m_oOStream.writeUTF("666:vid");
while( m_bVidActive ) {
while (m_oImage == null) {
m_oImage = m_oImgProc.getImage();
}

if( m_oImgIcon == null )
m_oImgIcon = new ImageIcon(m_oImage);

m_oOStream.writeUTF("666:1");
String str = m_oIStream.readUTF();
while( !str.equals("666:Go") )
str = m_oIStream.readUTF();
m_oObjOutStream.writeObject(m_oImgIcon);

m_oImgIcon = null;
m_oImage = null;

m_oObjOutStream.reset(); //this prevents memory leak
}
m_oOStream.writeUTF("666:0");
}


And this is the respective client side code:


else if( str.equals("666:vid")) {

m_bVidActive = true;

while( m_bVidActive ) {
str = m_oIStream.readUTF();
if( str.equals("666:1") ) {
m_oOStream.writeUTF("666:Go");
ImageIcon icon = (ImageIcon) m_oObjInStream.readObject();
m_oImgProc.setImage(icon);
m_oWorker.imageAvailable();
}
else if( str.equals("666:0") ) {
m_bVidActive = false;
}
}
}



What it does is simply transmitting images from server to client.
I still have trouble with stopping the transmission, cause as soon as, for example, the server stops and sends a message that the transmission is over, the client receives a malformed input error (obviously reading the messag via ObjectInputStream instead of datainputstream).

Anyway, the point is, that I inserted a message queue that makes the server send a message (666:1) when heīs ready to send a picture. Then he waits for the answer from the client that he is ready to receive it (666:Go). I thought it should work. Maybe thereīs an easier way, but I donīt know it. The problem, though, is the message from the client (666:Go). The server simply doesnīt react to it and deadlocks in the readUTF funktion.

I know there is this thing called blocking. Maybe itīs about that. But why does it not happen with the other messages?
And as long as I am at it, is there a way to ask a Stream if it has data waiting before actually using a read-method? Cause as soon as I am in it itīs too late if there never comes a message (or the wrong kind of message). I cannot get out of that funktion then anymore.

Cheers (and thanks again)

Oceansoul