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