HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 107
signalSIGALRM, handler
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 107 highlights
Using Internet Datagram Sockets Example Using Datagram Sockets exit(1); } servaddr_in.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr; /* Find the information for the "example" server * in order to get the needed port number. */ sp = getservbyname ("example", "udp"); if (sp == NULL) { fprintf(stderr, "%s: example not found in /etc/services\n", argv[0]); exit(1); } servaddr_in.sin_port = sp->s_port; again: /* Create the socket. */ s = socket (AF_INET, SOCK_DGRAM, 0); if (s == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to create socket\n", argv[0]); exit(1); } /* Bind socket to some local address so that the * server can send the reply back. A port number * of zero will be used so that the system will * assign any available port number. An address * of INADDR_ANY will be used so we do not have to * look up the internet address of the local host. */ myaddr_in.sin_family = AF_INET; myaddr_in.sin_port = 0; myaddr_in.sin_addr.s_addr = INADDR_ANY; if (bind(s, &myaddr_in, sizeof(struct sockaddr_in)) == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to bind socket\n", argv[0]); exit(1); } /* Set up alarm signal handler. */ signal(SIGALRM, handler); /* Send the request to the nameserver. */ if (sendto (s, argv[2], strlen(argv[2]), 0, &servaddr_in, sizeof(struct sockaddr_in)) == -1) { perror(argv[0]); fprintf(stderr, "%s: unable to send request\n", argv[0]); exit(1); } /* Set up a timeout so I don't hang in case the packet * gets lost. After all, UDP does not guarantee * delivery. */ alarm(5); /* Wait for the reply to come in. We assume that * no messages will come from any other source, * so that we do not need to do a recvfrom nor * check the responder's address. */ Chapter 4 107