TCP client sending buffer twice

I have an issue with the TCP client - sending data as Telnet.

My code appears to work if I use client.write( data), but is slow.
If I move to using client.write (bufferptr, buffersize), then I appear to be getting duplicate messages sent.

I’ll explain…

The Telnet code receives a ‘command’ packet from the network and automatically replies with a simple ‘T3’ string confirmation response, This received packet is parsed and sent to an SPI peripheral which causes another ‘answer’ response packet sent back over Telnet.
So we should have… ‘command’ … ‘T3’ … ‘answer’

What appears to be happening is the following… ‘command’ …‘previous answer’ … ‘T3’… ‘answer’

Each time the call is made to the functional calling client.write (bufferptr, buffersize), the bufferpointer is reset and size set to zero, so there seems to be a record of the data sent being held elsewhere and not cleared - perhaps in the tcp functions

There will be two possibly fast sequential calls to the client.write (bufferptr, buffersize) function, and I don’t know if this is permitted without some validity check.
The debug variables will only be showing the results from the second transmission because of the timings involved.

The duplicate message does not arise on the first attempt of sending a command.

b returns as -7009, which seems odd. ( no of bytes written )
R2 increments twice per command as expected ( ‘T3’ & ‘answer’ )
I also have a UDP transmission every 5 seconds which continues fine.

// Sends a globally defined array to TCP Client
void sendarray()
{
unsigned char *buffarrytemp;	/* ascii buffer pointer */
unsigned int n;
unsigned int b;

     R2++; // Cloud variable - count entry to this function debug
     R3 = arraysize; // cloud variable for debug

	#if true
	buffarrytemp = &array[0];
			for (n = 0; n <37 ;n++)
			{ cmdbuf4[n] = *buffarrytemp++ ;} // create cloud string for debug
	#endif

	  b = client.write( &array[0], arraysize); // This is the actual write

		R4 = b; // Inspect Cloud variable for debug
}

This is how the buffer is created and initialised

unsigned char serial_write0 (unsigned char byte)
{
	unsigned char tmp = 1;

       *arrayptr = byte;
	arrayptr++;
	arraysize++;
	return tmp; /* tmp is return code from fifo_enqueue()*/
}



void resetarray()
{

	arraysize = 0;
	arrayptr = &array[0];


}

Thanks

@rowifi, can you include the code where you define the global variables? Any reason you don’t use strcpy() or memcpy() to copy array[] to cmdbuf[]?

Hang fire … I may be onto it…

Hi, Typical just after posting my panic I spotted that I didn’t actually call the buffer reset function correcly.
My bad.

No reason about not using the strcpy or memcpy functions, I tend to like seeing things really basic.

Not sure about the other issues which may be worth knowing about

e.g the -7009 returned from the write, and if it’s valid to do fast sequential writes to the client write (Buffer , size ) function, i.e. does it make its own copy of the buffer and append to it, or is is accessing my live buffer at each call.

Thanks

@rowifi, murphy’s law of bugs! As for the client.write buffer use, I’ll have to ping @mdma for that. :smile: