Telemetry Data Formats 83
© 2017 Nortek AS
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
printf("Could not create socket %s\n\n", strerror(errno));
exit(-1);
}
memset((char *) &server, 0, sizeof(server));
/* Connect socket using name specified name / IP address. */
server.sin_family = AF_INET;
hp = gethostbyname(ip_address);
if (hp == 0) {
printf("Invalid host name\n\n");
exit(-1);
}
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons((unsigned short) ASCII_DATA_PORT);
/* 30 second receive timeout. The actual timeout to use will depend
* upon the instrument configuration and other considerations.
*/
#ifdef __WIN32__
/* On windows, the timeout is number of ms. */
int ts = 30000;
#else
/* Other OSes use timeval structure. */
struct timeval ts;
ts.tv_sec = 30;
ts.tv_usec = 0;
#endif
if (setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (void *) &ts, sizeof(ts))
< 0) {
printf("Could not set receive timeout\n\n");
exit(-1);
}
/* Connect to the instrument... */
if (connect(socket_fd, (struct sockaddr *) &server, sizeof(server)) < 0) {
printf("Could not connect to host %s\n\n", ip_address);
exit(-1);
}
int length = 0;
while (1) {
char c;
int r;
#ifdef __WIN32__
if ((r = recv(socket_fd, &c, 1, 0)) <= 0) {
if (r == 0) {
/* Instrument terminated socket for some reason. Re-connect required. */
printf("Instrument terminated socket.\n\n");
} else {