Datasheet

API in C
225
/********************************************************************
* Read a word from the socket
* The word that was read is returned as a string
********************************************************************/
char *readWord(int fdSock)
{
int iLen = readLen(fdSock);
int iBytesToRead = 0;
int iBytesRead = 0;
char *szWord;
char *szRetWord;
char *szTmpWord;
DEBUG ? printf("readWord iLen=%x\n", iLen) : 0;
if (iLen > 0)
{
// allocate memory for strings
szRetWord = calloc(sizeof(char), iLen + 1);
szTmpWord = calloc(sizeof(char), 1024 + 1);
while (iLen != 0)
{
// determine number of bytes to read this time around
// lesser of 1024 or the number of byes left to read
// in this word
iBytesToRead = iLen > 1024 ? 1024 : iLen;
// read iBytesToRead from the socket
iBytesRead = read(fdSock, szTmpWord, iBytesToRead);
// terminate szTmpWord
szTmpWord[iBytesRead] = 0;
// concatenate szTmpWord to szRetWord
strcat(szRetWord, szTmpWord);
// subtract the number of bytes we just read from iLen
iLen -= iBytesRead;
}
// deallocate szTmpWord