/* set needed socket options */ bool tune_socket(int sock, bool is_unix) { int res; int val; bool ok; /* * Generic stuff + nonblock. */ ok = socket_setup(sock, true); if (!ok) goto fail; /* * Following options are for network sockets */ if (is_unix) return true; /* * TCP Keepalive */ ok = socket_set_keepalive(sock, cf_tcp_keepalive, cf_tcp_keepidle, cf_tcp_keepintvl, cf_tcp_keepcnt); if (!ok) goto fail; /* * set in-kernel socket buffer size */ if (cf_tcp_socket_buffer) { val = cf_tcp_socket_buffer; res = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)); if (res < 0) goto fail; val = cf_tcp_socket_buffer; res = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)); if (res < 0) goto fail; } /* * Turn off kernel buffering, each send() will be one packet. */ val = 1; res = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); if (res < 0) goto fail; return true; fail: log_warning("tune_socket(%d) failed: %s", sock, strerror(errno)); return false; }
/* * Acepta un cliente en un socket a la escucha. */ int socket_accept(int serversock) { int ret; struct sockaddr sa; socklen_t slen = sizeof(struct sockaddr); ret = accept(serversock, (struct sockaddr *) &sa, &slen); socket_set_no_linger(ret); socket_set_keepalive(ret); return ret; }
int main(int argc, const char *argv[]) { #define OPEN 1 #define CLOSE 0 pthread_t show_data,wait_controller; int err,ret,alive; FD_ZERO(&wset); FD_ZERO(&rset); FD_SET(fd,&wset); FD_SET(fd,&rset); ss = fx_serial_start(); fd = client_init(7000,argv[1]); select(fd+1,&rset,&wset,NULL,NULL); restart: /*set keepalive on fd*/ alive = 1; if(setsockopt(fd,SOL_SOCKET,SO_KEEPALIVE,&alive,sizeof(alive)) != 0) { fprintf(stderr,"set socket option error\n"); goto restart; } socket_set_keepalive(fd); ret = connect_retry(fd,(struct sockaddr *)&clientaddr,sizeof(clientaddr)); if(ret == -1) { printf("cannot connect to server\n"); exit(1); } signal(SIGINT,free_space); err = pthread_create(&wait_controller,NULL,Control_controller,NULL); if(err != 0) { fprintf(stderr,"cannot create the thread\n"); exit(1); } while(1) { header__init(&MS.mh); sensor_info__init(&MS.si); sensor_t__init(&MS.st); Get_data(); //test(); sleep(293); //sleep 5 mins } //err = pthread_create(&show_data,NULL,Sensor_data,NULL); //if(err != 0) // { // fprintf(stderr,"cannot create the thread\n"); // exit(1); //} //pthread_join(show_data, NULL); pthread_join(wait_controller, NULL); //while(1) getchar(); return 0; }