EMBEDDED APPLICATIONS FCT
2.7
TCP
Send
Function
Script {TCP_send.sc}
It is important to note that when using tcps(), the intrinsic
TCP send function, the application-writer must check the
number of bytes sent on successful return from the
function, because a successful return is possible with the
number of bytes actually sent being less than the number
requested. In this case it is the responsibility of the
application-writer to ensure that the remainder of the data
buffer is re-submitted to the function.
The following is an example of a TCP send script function
that checks for and corrects problems when not all data
requested is sent by the tcps() function.
TCPSend( char cSckNum, char *cpTxBuffer, int *ipLen )
{
int MAX_TX_ATTEMPTS = 20;
int TX_FAILED = -2;
int TCP_BACKOFF_DLYS = 2;
int iResult = 0, iInitLen = *ipLen, iSent = 0, iTxAttempts = 0;
int
iReqLen;
char
*cpBuffer;
while ( (iSent != iInitLen) && (iResult == 0) )
{
iReqLen = iInitLen - iSent;
cpBuffer = cpTx iSent;
iResult = tcps( cSckNum, cpBuffer, &iReqLen );
while ( (iResult != 0) && (iTxAttempts < MAX_TX_ATTEMPTS) )
{
dlys(
TCP_BACKOFF_DLYS
);
iResult = tcps( cSckNum, cpBuffer, &iReqLen );
iTxA+;
}
if ( iTxAttempts >= MAX_TX_ATTEMPTS )
{
return
TX_FAILED;
}
iSent = iSent + iReqLen;
if ( iSent != iInitLen )
{
dlys(
TCP_BACKOFF_DLYS
);
}
iTxA+;
}
return
iResult;
}
The parameters for the call to TCPSend are the same as
for tcps(), but the length parameter is an input only. The
function will not return until either all the data has been
sent, or a failure has occurred. Obviously the values for
LZT 123 8019 R1A
14