Printing 0

12/14/2016

 

Page 47

Add 

#define _GNU_SOURCE

to the start of all programs that make use of the time.h header

i.e.page 47, page 271 and page 282

Program listings web page has been updated

Page 166

The printf and humidy calculation uses byte1 to byte5 before they have been defined. The order just needs to be changed:

Change

printf("Checksum %d %d \n\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
int byte1 = getByte(1, buf);
int byte2 = getByte(2, buf);
int byte3 = getByte(3, buf);
int byte4 = getByte(4, buf);
int byte5 = getByte(5, buf);
printf("Humidity= %f \n\r", humidity); 

 

To 


int byte1 = getByte(1, buf);
int byte2 = getByte(2, buf);
int byte3 = getByte(3, buf);
int byte4 = getByte(4, buf);
int byte5 = getByte(5, buf);
printf("Checksum %d %d \n\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
printf("Humidity= %f \n\r", humidity);

 Page 250

 The test:

If(maxbytes>=sizeof(buf)) return -1

is silly. It is an attempt to check that the buffer is big enough but sizeof will return the size of the pointer not the array.

The size of the array is implied in maxbytes so no test is needed -- delete the test and ensure that buf has at least maxbytes elements before calling the function.

Page 257

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>

#include <unistd.h>

Page 263

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

 You also need to change the inital part of the program to read:

 hints.ai_family =  AF_INET;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = AI_PASSIVE;
 getaddrinfo(NULL, "80", &hints, &server);
int sockfd = socket(server->ai_family, server->ai_socktype| SOCK_NONBLOCK, server->ai_protocol);

The change is to take SOCK_NONB from the call to getaddrinfo and move it to socket.