#include #include #include #include #include #include #include #include #include #include #include #include #include int getsecs(void) { struct timeval time; gettimeofday(&time, NULL); return time.tv_sec; } class UDPThread : public Thread { public: UDPThread() : Thread() { } protected: void Run(void) { int fd = 0; char buf[1024]; struct sockaddr_in src; bzero (&src, sizeof (src)); // src.sin_len = sizeof (src); src.sin_family = AF_INET; src.sin_port = htons(3000); src.sin_addr.s_addr = htonl(INADDR_ANY); if( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket "); return; } if( bind(fd, (struct sockaddr *)&src, sizeof(src)) < 0) { perror("bind "); return; } cout << "waiting" << endl; if( read(fd, buf, 1024) < 0){ perror("read "); return; } cout << "done recv" << endl; return; } }; class MyThread : public Thread { public: MyThread(int val=0) : Thread() { sleeptime = val;} protected: int sleeptime; void Run(void) { struct timeval time; time.tv_sec = sleeptime; time.tv_usec = 0; cout << "ID " << sleeptime << " I am running at " << getsecs() << endl; select(0,0,0,0,&time); cout << "ID " << sleeptime << " I am returning at " << getsecs() << endl; } }; int main(void) { MyThread thread1(5); MyThread thread2(7); MyThread thread3(9); UDPThread thread4; thread4.Start(); thread3.Start(); thread2.Start(); thread1.Start(); // sleep(2); Thread::Join(&thread4); return 0; }