Re: problem reading TCP packets on socket
- From: "Matt Humphrey" <matth@xxxxxxxxxxxxxx>
- Date: Tue, 6 Dec 2005 20:50:15 -0500
"antoine" <antoineducom@xxxxxxxxxxx> wrote in message
news:1133916969.517690.108180@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hello,
>
> I'm having a pretty annoying issue with a TCP connection in a
> client/server environment.
>
> my client connects to a server that sends it messages of variable
> lengths.
>
> on reception of each message, the client strips down the message in the
> following manner:
>
> 1. the first two characters of the message are first read to indicate
> the length of the message (as in the "decodeLength" method in the code
> below)
> 2. knowing the length of the message, I read the appropriate number of
> characters on my bufferedReader, and I start over.
<message and code snipped>
> try {
> char[] _rawMsg = new char[len];
> _bufferedReader.read(_rawMsg);
This read operation is not guaranteed to fill the buffer. Depending on how
the transport layer breaks up the original packets, it may return a partial
buffer. You must look at the number of characters returned and keep reading
until you get the whole thing--something like this:
while (len > 0) {
int didRead = _bufferedReader.read(_rawMsg, _rawMsg.length - len, len);
len -= didRead;
}
Cheers,
Matt Humphrey matth@xxxxxxxxxxxxxx http://www.iviz.com/
.
- References:
- problem reading TCP packets on socket
- From: antoine
- problem reading TCP packets on socket
- Prev by Date: Re: [Newbie Question] Magic Number's dangerous?
- Next by Date: Re: compression API available in Java & C++?
- Previous by thread: problem reading TCP packets on socket
- Next by thread: Re: problem reading TCP packets on socket
- Index(es):
Relevant Pages
|