tags:

views:

300

answers:

5

Hi Forum,

I am a newbie programmer.I have a problem as goes 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 the data with header character attached. if header ascii character is between 1h - ffh other than 0h works properly. I know that if null is added to string it consider as end of string. But i need to send NULL character with data through the socket. Can anybody please help me how to solve this problem.

Thank you in advance

+1  A: 
iTotSize = strlen(sendbuf);

strlen(sendbuf) will stop counting chars as soon as it finds the null char.

Count the total size by adding the various sizes manually.

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

Nick D
Hi, Nick Thankyou very much for your reply..you r genious
or just `iTotSize = iPos;`
mark4o
@mark4o, of course ;-) (even `iTotSize = sendsize + 1`, is fine)
Nick D
A: 

strlen stops at the NUL character, and does not count it. You can simply add one to that length

EvilTeach
HI EvilTech, thank you very much..
+4  A: 

If you want to avoid the Null termination, then you must stop treating your data as a string. strcpy is meant to copy only null-terminated strings. It is implemented to copy every byte until it encounters a #0. memcpy can copy any memory location. It is not bound by a null-terminated string. Since memcpy cannot determine the size of the data to be copied, you must provide that information. Additionally, you should not use strlen, as it is bound by the same termination rules.

Nescio
Hi Nescio , Thank you very much..you hav made my day..
+1  A: 

If you have to deal with data containing '\0', keep their sizes separately and use memcpy instead of strcpy or strncpy.

Note that in your example you have already calculated right length of the data packet when allocating memory to sendbuf. Just use that value (-1). Also, note that you ensured enough space for data in send buf, so you can use strcpy safely. strncpy does not terminate the output string when the limit is reached - prone to errors.

When working with size in C, use size_t type defined in stdlib.h instead of int.

Hope that 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);  
}
Viliam
A: 

As nobody pointed this out yet - you have a memory leak here. You need to call free(sendbuf); somewhere after you sent the bytes and before returning from the function.

There's also a possibility that sent() returns a short count - when TCP stack accepts less data then you gave it, i.e. socket send buffer is full - so you have to check the return value.

Nikolai N Fetissov