#include #include #include #include #include struct sockaddr_in addr_0, addr_1, addr_2 ; int fd0, fd1, rv0, rv1, addrlen; char buf_0[1024], buf_1[1024]; pthread_t thread; void* thread_rv; void* thread_main (void* arg) { int i; for ( i=0; i<3; i++ ) { addr_2.sin_family = 0; addr_2.sin_port = 0; addr_2.sin_addr.s_addr = 0; addrlen = sizeof addr_2; rv1 = recvfrom ( fd1, buf_0, sizeof buf_0, 0, (struct sockaddr*) &addr_2, &addrlen ); int fam = addr_2.sin_family; int port = addr_2.sin_port; int addr = addr_2.sin_addr.s_addr; printf ("\n"); perror ("thread recvfrom"); printf ( " rv %d addrlen %d fam %d port %d addr %x\n", rv1, addrlen, fam, ntohs (port), addr ); ;;; } return NULL; } void main_main () { send ( fd0, "hello", 6, 0); send ( fd0, "", 0, 0); usleep (100000); rv0 = shutdown ( fd1, SHUT_RD ); printf ("\n"); perror ("main shutdown"); printf (" %d\n", rv0) ;;; } int main () { addr_0.sin_family = AF_INET; addr_0.sin_port = htons (8000); inet_pton ( AF_INET, "127.0.0.1", &addr_0.sin_addr ); addr_1.sin_family = AF_INET; addr_1.sin_port = htons (8001); inet_pton ( AF_INET, "127.0.0.1", &addr_1.sin_addr ); fd0 = socket ( AF_INET, SOCK_DGRAM, 0 ); bind ( fd0, (struct sockaddr*) &addr_0, sizeof addr_0 ); connect ( fd0, (struct sockaddr*) &addr_1, sizeof addr_1 ); fd1 = socket ( AF_INET, SOCK_DGRAM, 0 ); bind ( fd1, (struct sockaddr*) &addr_1, sizeof addr_1 ); connect ( fd1, (struct sockaddr*) &addr_0, sizeof addr_0 ); pthread_create ( &thread, NULL, thread_main, NULL ); main_main (); pthread_join ( thread, &thread_rv ); return 0; }