int main(int argc, char** argv) { int file_number = 0; int udp_port = SERVPORT; char filename[128] = "test.dat"; char filename_new[256] = {0};
int res; pthread_t tid_sock; void *thread_result;
/* 命令行输入参数解析 */ if (argc < 2) { printf("Usage: \n"); printf("-p port : set listern port\n"); printf("-o file : set output filename\n"); printf("-h : print help infomation\n"); printf("eg :\n"); printf("$./udp_server -p 10001 -o test.dat\n"); exit(1); } parse_params(argc, argv, &udp_port, filename);
/* SIGNAL,键盘命令响应的初始化 */ int key; if (!using_stdin) { if (verbose >=0) printf("Press [q] to stop, [?] for help, [n] for creating new file\n"); } term_init(); /* end */
bTerminalFlag = 1; printf("Waiting for thread to finish...\n"); res = pthread_join(tid_sock, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined, it returned %s\n", (char*)thread_result);
memset(filename_new, 0, sizeof(filename_new)); fflush(fp_udp); fclose(fp_udp); } if (key == '?') { printf("key function\n" "? show this help\n" "n create new file\n" "q quit\n"); } } } /* end */
printf("Waiting for thread to finish...\n"); res = pthread_join(tid_sock, &thread_result); if (res != 0) { perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined, it returned %s\n", (char*)thread_result);
fflush(fp_udp); fclose(fp_udp);
return 0; }
void *sock_recv_udp(void *arg) { int port = *(int*)arg;
int sockfd, recvbytes; char rcv_buf[MAXDATASIZE], snd_buf[MAXDATASIZE]; struct sockaddr_in server_addr; socklen_t addr_size=sizeof(server_addr); int pkt_cnt = 0;
/* receive the data from "server_addr" by "sockfd" and store into "rcv_buf" */ if ((recvbytes = recvfrom(sockfd, rcv_buf, MAXDATASIZE, 0, (struct sockaddr*)&server_addr, &addr_size)) == -1) { perror("recv:"); exit(1); }