Re: First message sent is lost (TCP)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



WP <mindcooler@xxxxxxxxx> wrote:
I strongly suggest you design your protocol so that each message is
prefixed by its length, so it's easy to tell when any particular
message has finished. Don't start looking at the message until you've
got that much data.

Thanks Jon for your quick reply!
I did something slightly different, let me describe it: I introduced a
special symbol (I choose $, it's not used for anything else) to
signify the end of a message.

Personally I prefer the length-prefix method. It can be pain if you
want to start sending a message before you know how long it is, but it
makes it much easier to make sure you've got all the data before you
start trying to do anything with it.

Each time my read callback is called, I
extract all full messages. I also keep track of uncompleted messages.
The code doesn't currently handle if a message isn't completed the
next time the callback is called but I put an assertion for that case
for now and it hasn't triggered (shouldn't trigger very likely).
I had some problems first with message corruption that was very weird,
for example if I put a Trace.WriteLine() at sender it got worse on the
receiving end, but after making sure I send and receive a fixed-size
byte array and clear the receiving array each time those problems
disapperared.

Don't forget that just because you send and receive a certain size of
array doesn't mean you'll actually get that much data.

Here's the read callback now, it's not very efficient, but at least it
seems to work now and I can continue implementing the actual card
game, heh:

private void OnReadMessage(IAsyncResult ar)
{
int total_bytes_read = this.tcp_client.GetStream().EndRead(ar);
String input = Encoding.ASCII.GetString(this.message, 0,
message.Length).Trim('\0');

No - only decode the number of bytes you've read.

<snip>

My next problem I need to tackle is the exceptions (IOExceptions
mostly I think) I get after closing the server or the client after
they have started communicating with each other. Any tips on that?

I would hope that EndRead would return 0 - that's how you should detect
it.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
.



Relevant Pages

  • Re: EDMA Event-Triggered Synchronization
    ... Array Synchronization? ... the EDMA controller will transfer one ... Note that the parameter set gets modified during the transfer so ... trigger the transfer request. ...
    (comp.dsp)
  • Re: EDMA Event-Triggered Synchronization
    ... Array Synchronization? ... Note that the parameter set gets modified during the transfer so ... trigger the transfer request. ...
    (comp.dsp)
  • Re: EDMA Event-Triggered Synchronization
    ... Array Synchronization? ... the EDMA controller will transfer one ... Note that the parameter set gets modified during the transfer so ... trigger the transfer request. ...
    (comp.dsp)
  • Re: Curve Fitting of Real Data
    ... (but the noise array is 128 elements), resulting in a 50 element array ... and is supposed to return 1000 points after the trigger, ... being passed to the fitting routine is still only 50 points long, ...
    (comp.lang.labview)