1 /*
2  * Copyright (c) 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1986 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)dgramsend.c	6.3 (Berkeley) 03/07/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 
34 #define DATA "The sea is calm tonight, the tide is full . . ."
35 
36 /*
37  * Here I send a datagram to a receiver whose name I get from the command
38  * line arguments.  The form of the command line is dgramsend hostname
39  * portnumber
40  */
41 
42 main(argc, argv)
43 	int argc;
44 	char *argv[];
45 {
46 	int sock;
47 	struct sockaddr_in name;
48 	struct hostent *hp, *gethostbyname();
49 
50 	/* Create socket on which to send. */
51 	sock = socket(AF_INET, SOCK_DGRAM, 0);
52 	if (sock < 0) {
53 		perror("opening datagram socket");
54 		exit(1);
55 	}
56 	/*
57 	 * Construct name, with no wildcards, of the socket to send to.
58 	 * Getnostbyname() returns a structure including the network address
59 	 * of the specified host.  The port number is taken from the command
60 	 * line.
61 	 */
62 	hp = gethostbyname(argv[1]);
63 	if (hp == 0) {
64 		fprintf(stderr, "%s: unknown host\n", argv[1]);
65 		exit(2);
66 	}
67 	bcopy(hp->h_addr, &name.sin_addr, hp->h_length);
68 	name.sin_family = AF_INET;
69 	name.sin_port = htons(atoi(argv[2]));
70 	/* Send message. */
71 	if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0)
72 		perror("sending datagram message");
73 	close(sock);
74 }
75