Adding data to a null character in a character array to send data over a socket

I am a newbie programmer. I have a problem like below,

void SockSend()  
{  
char *sendbuf;  
int sendsize;   /* send data size(variable size)*/  
int iPos = 0, iTotSize;  
char hdr;  
char *data = "ABCDEFGHIJKLMNO";   /* its just example, data can be any thing */  
sendsize = strlen(data);  

hdr = '\0';   /* header character */  
sendbuf = (char*)malloc(sendsize + 2);  
sendbuf[iPos] = hdr;  
iPos++;  
strncpy(sendbuf + iPos, data, 15);  
iPos += sendsize;  
sendbuf[iPos] = '\0';   /* append null at end of string*/  

iTotSize = strlen(sendbuf);  

send(sockid, sendbuf, iTotSize, 0);  
}

      

As in the above code, I need to send data with a header symbol attached. if the ascii header character is between 1h - ffh other than 0h works correctly. I know that if null is added to a string, it is treated as the end of the string. But I need to send a NULL character with data over a socket. Can anyone please help me to solve this problem.

Thank you in advance

+2


source to share


5 answers


If you want to avoid Null termination, you must stop processing your data as a string. strcpy is for copying only null-character strings. It is implemented to copy each byte until it encounters # 0. memcpy can copy any memory location. It is not associated with a descending string. Since memcpy cannot figure out the size of the data to be copied, you must provide this information. Also, you shouldn't use strlen as it is bound by the same interrupt rules.



+4


source


iTotSize = strlen(sendbuf);

      

strlen(sendbuf)

stops counting characters as soon as it finds zero char.



Calculate the total size by manually adding different sizes.

Maybe this can help: iTotSize = 1 + strlen(sendbuf + 1);

+1


source


If you need to deal with data containing '\0'

, store their sizes separately and use memcpy

instead of strcpy

or strncpy

.

Note that in your example, you have already calculated the correct data packet length when allocating memory to sendbuf

. Just use this value (-1). Also note that you have provided enough space for the data in send buf so that you can safely use strcpy

. strncpy

does not terminate the output line when the limit is reached - error prone.

When working with size in C, use the size_t

type defined in stdlib.h

instead int

.

Hope it helps ...

void SockSend()  
{  
    char *sendbuf;  
    int sendsize;   /* send data size(variable size)*/  
    int iPos = 0, iTotSize;  
    char hdr;  
    char *data = "ABCDEFGHIJKLMNO"; 
    sendsize = strlen(data);  /* -- Are you sure that data will not contain \0 ? */

    hdr = '\0';   /* header character */
    sendbuf = (char*)malloc(sendsize + 2);  /* -- Data size calculation! */
    sendbuf[iPos] = hdr;  
    iPos++;  
    strcpy(sendbuf + iPos, data);  
    iPos += sendsize;  
    sendbuf[iPos] = '\0';   /* append null at end of string*/  

    iTotSize = strlen(sendbuf);  

    send(sockid, sendbuf, iTotSize, 0);  
}

      

+1


source


strlen stops at the NUL character and ignores it. You can just add one to this length

0


source


Like no one has pointed this out yet - you have a memory leak ... You need to call free(sendbuf);

somewhere after sending bytes and before returning from the function.

There is also a possibility that a short count is sent()

returned - when the TCP stack receives less data than you issued it, i.e. the socket buffer is being sent, so you need to check the return value.

0


source







All Articles