#include #include #include #include #include #include #include #include #include int sock = -1; void * thread(void *arg) { printf("Sleeping\n"); sleep(1); printf("Closing\n"); close(sock); /* * Open as many sockets as possible to make it likely that the memory * for the socket that we just closed will be re-used. */ while (1) { int s = socket(PF_INET, SOCK_DGRAM, 0); if (s < 0) { perror("socket failed"); exit(1); } } return (NULL); } int main(int argc, char **argv) { sock = socket(PF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("socket failed"); exit(1); } printf("sock is %d\n", sock); struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(11965); sin.sin_addr.s_addr = htonl(0x7f000001); sin.sin_len = sizeof(sin); if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) { perror("connect failed"); exit(1); } pthread_t td; int error = pthread_create(&td, NULL, thread, NULL); if (error) { perror("pthread_create"); exit(1); } while (1) { struct pollfd pfd; pfd.fd = sock; pfd.events = POLLIN; printf("Polling...\n"); error = poll(&pfd, 1, 7000); if (error) { perror("poll failed"); exit(1); } } return (0); }