static void* handle_thread(void* data) { struct fd_set fds; struct timeval timeout = {3,0}; int max_fd = 0; char recv_buf[512] = ""; char send_buf[512] = ""; int *m_socket = malloc(sizeof(int)); *m_socket = *(int*)data; int s_socket = *m_socket; int len; while(1) { FD_ZERO(&fds); FD_SET(s_socket, &fds); max_fd = s_socket + 1; if(select(max_fd, &fds, NULL, NULL, &timeout) <= 0) continue; if(FD_ISSET(s_socket, &fds)) { bzero(recv_buf, sizeof(recv_buf)); len = recv(s_socket, recv_buf, sizeof(recv_buf), 0); if(len <=0 ) break; printf("[Server]: has recv bytes:%d, content:%s\n", len, recv_buf); int value = atoi(recv_buf); int res = square_int(value); bzero(send_buf, sizeof(send_buf)); len = snprintf(send_buf, sizeof(send_buf), "%d", res); len = send(s_socket, send_buf, len, 0); printf("Has sent result:%d, len:%d\n", res, len); } } free(m_socket); m_socket = NULL; return NULL; }
Value distance2( int x1, int y1, int z1, int x2, int y2, int z2 ) { return square_int(x1 - x2) + square_int(y1 - y2) + square_int(z1 - z2); }