As @Roddy pointed out, code is bad for it overwrites the string's \0
with the checksum, then tries to find the string length in the next line of a potentially unterminated string!
buff2s[strlen(buff2s)]=calculatedCS; //adds a checksum
buff2s[strlen(buff2s)]='\0'; //adds a new string terminator
Suggest:
size_t len = strlen(buff2s);
calculatedCS = checkSum(buff2s, len);
buff2s[len] = calculatedCS; //adds a checksum
buff2s[++len] = '\0'; //adds a new string terminator
bytes_written=write(fd, buff2s, len + 1);
Since working with strings, change checksum generator and checker to insure it does not create a '\0'
.
unsigned char checkSum(char buff[], size_t nbrOfBytes){
size_t ic;
unsigned int t_cSum = 0;
for (ic=0; ic<nbrOfBytes-1; ic++){
t_cSum += buff[ic];
}
return (unsigned char) (t_cSum % 255 + 1);
}
RegularReceive()
will not work. Suggest reading via fgetc()
until a '\0'
occurs. (or timeout or buffer full). In particular the following is dangerous as it is not known that buffR
ends with a '\0'
and the next function is strlen(buffR)
.
bytes_read = read(fd, buffR, sizeof(buffR)-1);
if(strlen(buffR)<1){