So are you for sure always sending MAXDATASIZE bytes? I mean, is the file you're sending always that length? If not, then that's your problem. You need to write() exactly the same number of bytes as is the length of the file you're sending. You can get this information from the return value of fread().
And when your client read()s the data from the socket, read() returns the number of bytes received and that's where you should be null-terminating the read buffer (i.e. bytes_received = read(sockfd, buf, MAXDATASIZE); buf[bytes_received] = '\0'; )