Zee, there is a fair amount of C++ or C99 code in your examples - chiefly the declaration of variables in the middle of statement blocks.

> numbytes = read(sockfd,buf,MAXDATASIZE);
> buf[numbytes] = '\0';
is a buffer overflow waiting to happen. If read() fills the buffer, then your \0 is outside the buffer

numbytes = read(sockfd,buf,MAXDATASIZE-1);
buf[numbytes] = '\0';

This is very different from say fgets() which automatically stores a \0 within the available space, so
fgets( buff, sizeof buff, fp )
is always good.