Connection reset

I am getting this error when reading from a Stream.

Basically the sequence of events goes like this:

I open a socket, write to the outputStream, read from the inputStream, write to the outputStream again and when reading I get this exception.

What happens if I do the following:

write to the outputStream -- the server replies

write to the outputStream -- the server replies once again

read the inputStream

Will I be reading the first reply from the server, the second or all the stream?

And is there any way that I can reset the connection, like cleaning the streams?

Thanks,

MeTitus

[651 byte] By [Me_Titusa] at [2007-11-27 10:22:34]
# 1

What I don't seem to understand is why I get this error after reading the stream twice. What it looks to me is that since I am reading again any content still on the stream is empty, and this exception is raised, but if so why isn't there a method for that.

Any help in this matter is really appreciated.

MeTitus

Me_Titusa at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...
# 2

This usually happens when the other end closes the connection without reading everyting you have written to it.

ejpa at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...
# 3

> What happens if I do the following:

>

> write to the outputStream -- the server replies

> write to the outputStream -- the server replies once

> again

> read the inputStream

>

> Will I be reading the first reply from the server,

> the second or all the stream?

You will be reading 'all the stream'. Streams do not preserve 'message' boundaries.

Another situation that can happen, if server replies are large enough, is that you read only half (or some small part) whole reply in 1 inputStream.read call.

It is the responsibility of the programer to somehow read meaningfull messages from the continuous stream of bytes that will be coming trough the socket (or some other source).

boruthadzialica at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...
# 4

> This usually happens when the other end closes the

> connection without reading everyting you have written

> to it.

I check the socket status, and it is sill open...

MeTitus

Me_Titusa at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...
# 5

> > What happens if I do the following:

> >

> > write to the outputStream -- the server replies

> > write to the outputStream -- the server replies

> once

> > again

> > read the inputStream

> >

> > Will I be reading the first reply from the server,

> > the second or all the stream?

>

> You will be reading 'all the stream'. Streams do not

> preserve 'message' boundaries.

>

> Another situation that can happen, if server replies

> are large enough, is that you read only half (or some

> small part) whole reply in 1 inputStream.read call.

>

> It is the responsibility of the programer to somehow

> read meaningfull messages from the continuous stream

> of bytes that will be coming trough the socket (or

> some other source).

It makes sence,

thanks,

MeTitus

Me_Titusa at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...
# 6

> This usually happens when the other end closes the

> connection without reading everyting you have written

> to it.

In this case I am not writing nothing to it at all:

while((character = this.objInputStream.read()) != -1)

{

if(pContent.length >= 1)

{

byte []byteTmp = new byte[pContent.length + 1];

System.arraycopy(pContent, 0, byteTmp, 0, pContent.length);

byteTmp[pContent.length - 1] = (byte)character;

pContent = byteTmp;

}

else

{

pContent = new byte[]{(byte)character};

}

}

}

MeTitus

Me_Titusa at 2007-7-28 17:16:50 > top of Java-index,Java Essentials,Java Programming...