1*62783Sbostic .\" Copyright (c) 1986, 1993
2*62783Sbostic .\"	The Regents of the University of California.  All rights reserved.
348154Sbostic .\"
448154Sbostic .\" %sccs.include.redist.roff%
548154Sbostic .\"
6*62783Sbostic .\"	@(#)dgramsend.c	8.1 (Berkeley) 06/08/93
748154Sbostic .\"
827700Smckusick #include <sys/types.h>
927700Smckusick #include <sys/socket.h>
1027700Smckusick #include <netinet/in.h>
1127700Smckusick #include <netdb.h>
1227700Smckusick #include <stdio.h>
1327700Smckusick 
1427700Smckusick #define DATA "The sea is calm tonight, the tide is full . . ."
1527700Smckusick 
1627700Smckusick /*
1727700Smckusick  * Here I send a datagram to a receiver whose name I get from the command
1827700Smckusick  * line arguments.  The form of the command line is dgramsend hostname
1927700Smckusick  * portnumber
2027700Smckusick  */
2127700Smckusick 
2227700Smckusick main(argc, argv)
2327883Skarels 	int argc;
2427883Skarels 	char *argv[];
2527700Smckusick {
2627883Skarels 	int sock;
2727700Smckusick 	struct sockaddr_in name;
2827700Smckusick 	struct hostent *hp, *gethostbyname();
2927700Smckusick 
3027700Smckusick 	/* Create socket on which to send. */
3127700Smckusick 	sock = socket(AF_INET, SOCK_DGRAM, 0);
3227700Smckusick 	if (sock < 0) {
3327700Smckusick 		perror("opening datagram socket");
3427883Skarels 		exit(1);
3527700Smckusick 	}
3627700Smckusick 	/*
3727700Smckusick 	 * Construct name, with no wildcards, of the socket to send to.
3827700Smckusick 	 * Getnostbyname() returns a structure including the network address
3927700Smckusick 	 * of the specified host.  The port number is taken from the command
4027700Smckusick 	 * line.
4127700Smckusick 	 */
4227700Smckusick 	hp = gethostbyname(argv[1]);
4327883Skarels 	if (hp == 0) {
4427883Skarels 		fprintf(stderr, "%s: unknown host\n", argv[1]);
4527883Skarels 		exit(2);
4627883Skarels 	}
4727883Skarels 	bcopy(hp->h_addr, &name.sin_addr, hp->h_length);
4827700Smckusick 	name.sin_family = AF_INET;
4927700Smckusick 	name.sin_port = htons(atoi(argv[2]));
5027700Smckusick 	/* Send message. */
5127883Skarels 	if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0)
5227700Smckusick 		perror("sending datagram message");
5327700Smckusick 	close(sock);
5427700Smckusick }
55