HP Rp3440-4 - 9000 - 0 MB RAM Programmer's Manual page 107

Bsd sockets interface programmer’s guide
Hide thumbs Also See for Rp3440-4 - 9000 - 0 MB RAM:
Table of Contents

Advertisement

exit(1);
}
servaddr_in.sin_addr.s_addr = ((struct in_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",
exit(1);
}
servaddr_in.sin_port = sp->s_port;
/* 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.
* of zero will be used so that the system will
* assign any available port number.
* 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. */
again:
if (sendto (s, argv[2], strlen(argv[2]), 0, &servaddr_in,
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.
* delivery.
*/
alarm(5);
/* Wait for the reply to come in.
* 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
(hp->h_addr))->s_addr;
argv[0]);
sizeof(struct sockaddr_in)) == -1) {
After all, UDP does not guarantee
Using Internet Datagram Sockets
Example Using Datagram Sockets
A port number
An address
We assume that
107

Advertisement

Table of Contents
loading

Table of Contents