#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netdb.h> #include <errno.h> #include <pthread.h> #include <sys/time.h> int sock; void *recv_thread(void); void *timer_thread(void); struct sockaddr_in r_sa; int main(int argc, char *argv[]) { struct sockaddr_in sa, s_sa; unsigned char sBuf[256]; ssize_t bytes_sent, sBuf_len; char c; if(argc == 1) sBuf_len=snprintf(sBuf, sizeof sBuf, "12345"); // inet_pton(AF_INET, "10.21.11.180", &s_sa.sin_addr); //else if (argc == 2) // inet_pton(AF_INET, argv[1], &s_sa.sin_addr); else sBuf_len=snprintf(sBuf, sizeof sBuf, argv[1]); sock = socket(PF_INET, SOCK_DGRAM, 0); bzero(&sa, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(17654); r_sa.sin_addr.s_addr = htonl(INADDR_ANY); if (-1 == bind(sock,(struct sockaddr *)&sa, sizeof(sa))) { perror("error bind failed"); close(sock); exit(EXIT_FAILURE); } bzero(&r_sa, sizeof(r_sa)); r_sa.sin_family = AF_INET; r_sa.sin_port = htons(17654); r_sa.sin_addr.s_addr = htonl(INADDR_ANY); bzero(&s_sa, sizeof(s_sa)); s_sa.sin_family = AF_INET; s_sa.sin_port = htons(17654); //s_sa.sin_addr.s_addr = htonl(0xe0000101); inet_pton(AF_INET, "224.0.0.200", &s_sa.sin_addr); //inet_pton(AF_INET, "10.21.11.180", &s_sa.sin_addr); int tRes; pthread_t rThread, tThread; tRes = pthread_create(&rThread, NULL, recv_thread, NULL); if (tRes != 0){ perror("Thread creation failed"); exit(EXIT_FAILURE); } tRes = pthread_create(&tThread, NULL, timer_thread, NULL); if (tRes != 0){ perror("Thread creation failed"); exit(EXIT_FAILURE); } while(1) { //system("pause"); //sBuf_len=fgets(sBuf, sizeof sBuf, stdin)) // //sBuf_len=gets(sBuf); //scanf("%s", sBuf); //printf("\npress enter to send data.\n"); c=getchar(); bytes_sent = sendto(sock, sBuf, sBuf_len, 0,(struct sockaddr*)&s_sa, sizeof(s_sa)); if (bytes_sent < 0) fprintf(stderr, "%s\n", strerror(errno)); int i; printf("sent data: "); for (i=0;i<bytes_sent;i++) printf("%c", sBuf[i]); printf("\n"); } return 0; } void *recv_thread() { unsigned char rBuf[256]; ssize_t bytes_recv; socklen_t fromlen; int i; struct timeval start, tv; int recv; int time; bytes_recv = recvfrom(sock, (void *)rBuf, 256, 0, (struct sockaddr *)&r_sa, &fromlen); gettimeofday(&start, NULL ); if (bytes_recv < 0) fprintf(stderr, "%s\n", strerror(errno)); printf("1.\ttime:0000000\tsequence:%d\n", rBuf[bytes_recv-1]); recv=1; //printf("\npress enter to send data.\n"); while(1) { bytes_recv = recvfrom(sock, (void *)rBuf, 256, 0, (struct sockaddr *)&r_sa, &fromlen); gettimeofday(&tv, NULL ); if (bytes_recv < 0) fprintf(stderr, "%s\n", strerror(errno)); time = (tv.tv_sec-start.tv_sec)*1000000+tv.tv_usec-start.tv_usec; recv++; printf("%d.\ttime:%d\tsequence:%d\n", recv, time, rBuf[bytes_recv-1]); /* printf("\ntime:%d sequence:%d length:%d data: ", (tv.tv_sec-start.tv_sec)*1000000+tv.tv_usec-start.tv_usec, rBuf[bytes_recv-1], bytes_recv); for (i=0;i<bytes_recv;i++) printf("%x ",rBuf[i]); printf("\n"); */ //printf("\npress enter to send data.\n"); } } void *timer_thread() { struct timeval start, tv; gettimeofday(&start, NULL ); while(1) { gettimeofday(&tv, NULL ); if( ((tv.tv_sec-start.tv_sec)*1000000+tv.tv_usec-start.tv_usec) > 30300000000) exit(1); } }