
API In CPP
127
}
int retMessageLength = *messageLength;
delete messageLength;
delete [] lengthData;
return retMessageLength;
}
/********************************************************************
* Read a word from the socket
* The word that was read is returned as a string
********************************************************************/
void MikrotikAPI::ReadWord(string &strWordOut)
{
int messageLength = ReadLength();
int bytesToRead = 0;
int bytesRead = 0;
char *tmpWord;
DEBUG ? printf("ReadWord messageLength=%x\n", messageLength) : 0;
strWordOut.clear();
if (messageLength > 0) {
// allocate memory for strings
tmpWord = (char *) calloc(sizeof(char), 1024 + 1);
while (messageLength != 0) {
// determine number of bytes to read this time around
// lesser of 1024 or the number of byes left to read
// in this word
bytesToRead = messageLength > 1024 ? 1024 : messageLength;
// read iBytesToRead from the socket
bytesRead = read(fdSock, tmpWord, bytesToRead);
// terminate szTmpWord
tmpWord[bytesRead] = 0;
// concatenate szTmpWord to szRetWord
strWordOut += tmpWord;
// subtract the number of bytes we just read from iLen
messageLength -= bytesRead;
}
Kommentare zu diesen Handbüchern