Sending NULL character ('\0') over a TCP connection

Greetings,

Intro: I am developing a TCP Server (TCP Passive Open) using LPC2364 (ARM7TDMI) using the old RL_TCPNet library. I need to send a string of ASCII data, which contains random characters with ASCII values ranging from 0 to 255 (ie. in hex 0x00 to 0xFF), over the TCP connection. 

Issue: When I try to send a string which contains random ASCII characters, the data gets terminated with the NULL character in between and is partially transmitted.

In the RL_TCPNet library which I am using, I send the data in the following way,

unsigned char TCPSocket;
unsigned char *TCPTxBuffer;
unsigned short int TxBufferSize = 15;
char data_buffer[20] = {'a','1','h','8','$','_','_','\0','-','-','k','e','i','l'};

main()
{
    int i=0;
    for(i=0;i<15;i++)
    {
        TCPTxBuffer[i]=data_buffer[i];
        printf("%c",TCPTxBuffer[i]);
    }
    
    //After TCP connection establishment 
    
    tcp_send(TCPSocket, *TCPTxBuffer, TxBufferSize);
    
    //tcp_send(unsigned char, unsigned char *, unsigned short int) - Library function which is used to send data over TCP
    
    //TCPSocket - TCP Socket Identifier
    
    //TCPTxBuffer - TCP transmit buffer
    
    //TxBufferSize - TCP transmit buffer size.
}

Ex:

Data to be sent :  a1h8$__\0--keil   Data Length: 15

Data received : a1h8$__

I understand this condition is obvious with C language. But, the printf statement which I have used in the code is able to print all the 15 characters as it has a for loop  iteration count as 15. 

Similarly, I have specified the data length(TxBufferSize) to be the actual length of the string (ie. 15). Then why does partial transmission takes places, still it gets terminated by NULL. 

NOTE: The tcp_send function in the RL_TCPNet library is inaccessible and cannot be modified.

Requirement: In what ways could I transmit the full string of data over the TCP connection. Sending the whole data is crucial and preferably in ASCII format.

Kindly help me out in this regard. Thanks in advance.

Parents
  • a string which contains random ASCII characters, the data gets terminated with the NULL character

    Of course it does - that is the defined behaviour of the string functions in the 'C' Standard!

    If you want to send arbitrary data - which may contain NULs - then you cannot use 'C' string functions!

    However, this has nothing specifically to do with TCP/IP.

    Are you sure that the problem is actually in the TCP/IP, or just in your debug stuff?

    for example:

        for(i=0;i<15;i++)
        {
            TCPTxBuffer[i]=data_buffer[i];
            printf("%c",TCPTxBuffer[i]);
        }
        
        //After TCP connection establishment 
        
        tcp_send(TCPSocket, *TCPTxBuffer, TxBufferSize);

    printf() is a 'C' string function - so it will stop printing at the first NUL - but that does not mean that the tcp_send() will not send the data beyond the NUL ...

    Perhaps what you really want is something like:

        for( i=0; i<15; i++ )
        {
            TCPTxBuffer[i] = data_buffer[i];
            printf( "%02X ", TCPTxBuffer[i] );
        }

Reply
  • a string which contains random ASCII characters, the data gets terminated with the NULL character

    Of course it does - that is the defined behaviour of the string functions in the 'C' Standard!

    If you want to send arbitrary data - which may contain NULs - then you cannot use 'C' string functions!

    However, this has nothing specifically to do with TCP/IP.

    Are you sure that the problem is actually in the TCP/IP, or just in your debug stuff?

    for example:

        for(i=0;i<15;i++)
        {
            TCPTxBuffer[i]=data_buffer[i];
            printf("%c",TCPTxBuffer[i]);
        }
        
        //After TCP connection establishment 
        
        tcp_send(TCPSocket, *TCPTxBuffer, TxBufferSize);

    printf() is a 'C' string function - so it will stop printing at the first NUL - but that does not mean that the tcp_send() will not send the data beyond the NUL ...

    Perhaps what you really want is something like:

        for( i=0; i<15; i++ )
        {
            TCPTxBuffer[i] = data_buffer[i];
            printf( "%02X ", TCPTxBuffer[i] );
        }

Children
More questions in this forum