Re: problem reading TCP packets on socket




"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/


.



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Client Server socket behavior on XP different for 127.0.0.1
    ... > be sure that the characters you send are the characters you receive. ... This splitting and concatenating is affected by network ... The error message/traceback from the server log file is as follows: ... The server gets the same exception it would get for a client ...
    (comp.lang.java.programmer)
  • Re: UCX connect function timeout
    ... This is an example of a VMS client program communicating with ... In this example the T3_DEMO server is listening on port 1024 at node ... The Access Control Information buffer must be the first buffer transmitted ... the communication server. ...
    (comp.os.vms)
  • Re: out param question
    ... even though the server side set's the pSize on the way out, ... it back to the client, ... the client would free the buffer ... ... what IDL am i looking for here? ...
    (microsoft.public.win32.programmer.ole)
  • Re: Buffersize for Socket.Recieve() method
    ... > i have a problem with the buffer size at the client. ... > Server -> Server sends 100 bytes of data ... oder ebenso mit vorherigen/nachfolgenden Daten kombiniert [zB Recv liefert 110 Bytes aufs mal]. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)