1 .\" Copyright (c) 1986, 1993
2 .\"	The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %sccs.include.redist.roff%
5 .\"
6 .\"	@(#)dgramsend.c	8.1 (Berkeley) 06/08/93
7 .\"
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <stdio.h>
13 
14 #define DATA "The sea is calm tonight, the tide is full . . ."
15 
16 /*
17  * Here I send a datagram to a receiver whose name I get from the command
18  * line arguments.  The form of the command line is dgramsend hostname
19  * portnumber
20  */
21 
22 main(argc, argv)
23 	int argc;
24 	char *argv[];
25 {
26 	int sock;
27 	struct sockaddr_in name;
28 	struct hostent *hp, *gethostbyname();
29 
30 	/* Create socket on which to send. */
31 	sock = socket(AF_INET, SOCK_DGRAM, 0);
32 	if (sock < 0) {
33 		perror("opening datagram socket");
34 		exit(1);
35 	}
36 	/*
37 	 * Construct name, with no wildcards, of the socket to send to.
38 	 * Getnostbyname() returns a structure including the network address
39 	 * of the specified host.  The port number is taken from the command
40 	 * line.
41 	 */
42 	hp = gethostbyname(argv[1]);
43 	if (hp == 0) {
44 		fprintf(stderr, "%s: unknown host\n", argv[1]);
45 		exit(2);
46 	}
47 	bcopy(hp->h_addr, &name.sin_addr, hp->h_length);
48 	name.sin_family = AF_INET;
49 	name.sin_port = htons(atoi(argv[2]));
50 	/* Send message. */
51 	if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0)
52 		perror("sending datagram message");
53 	close(sock);
54 }
55