Commit | Line | Data |
---|---|---|
af359dea C |
1 | .\" Copyright (c) 1986 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
c0567266 KB |
4 | .\" Redistribution and use in source and binary forms, with or without |
5 | .\" modification, are permitted provided that the following conditions | |
6 | .\" are met: | |
7 | .\" 1. Redistributions of source code must retain the above copyright | |
8 | .\" notice, this list of conditions and the following disclaimer. | |
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer in the | |
11 | .\" documentation and/or other materials provided with the distribution. | |
12 | .\" 3. All advertising materials mentioning features or use of this software | |
13 | .\" must display the following acknowledgement: | |
14 | .\" This product includes software developed by the University of | |
15 | .\" California, Berkeley and its contributors. | |
16 | .\" 4. Neither the name of the University nor the names of its contributors | |
17 | .\" may be used to endorse or promote products derived from this software | |
18 | .\" without specific prior written permission. | |
19 | .\" | |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | .\" SUCH DAMAGE. | |
af359dea C |
31 | .\" |
32 | .\" @(#)dgramsend.c 6.4 (Berkeley) 4/17/91 | |
33 | .\" | |
23237d2e KM |
34 | #include <sys/types.h> |
35 | #include <sys/socket.h> | |
36 | #include <netinet/in.h> | |
37 | #include <netdb.h> | |
38 | #include <stdio.h> | |
39 | ||
40 | #define DATA "The sea is calm tonight, the tide is full . . ." | |
41 | ||
42 | /* | |
43 | * Here I send a datagram to a receiver whose name I get from the command | |
44 | * line arguments. The form of the command line is dgramsend hostname | |
45 | * portnumber | |
46 | */ | |
47 | ||
48 | main(argc, argv) | |
a43acb6b MK |
49 | int argc; |
50 | char *argv[]; | |
23237d2e | 51 | { |
a43acb6b | 52 | int sock; |
23237d2e KM |
53 | struct sockaddr_in name; |
54 | struct hostent *hp, *gethostbyname(); | |
55 | ||
56 | /* Create socket on which to send. */ | |
57 | sock = socket(AF_INET, SOCK_DGRAM, 0); | |
58 | if (sock < 0) { | |
59 | perror("opening datagram socket"); | |
a43acb6b | 60 | exit(1); |
23237d2e KM |
61 | } |
62 | /* | |
63 | * Construct name, with no wildcards, of the socket to send to. | |
64 | * Getnostbyname() returns a structure including the network address | |
65 | * of the specified host. The port number is taken from the command | |
66 | * line. | |
67 | */ | |
68 | hp = gethostbyname(argv[1]); | |
a43acb6b MK |
69 | if (hp == 0) { |
70 | fprintf(stderr, "%s: unknown host\n", argv[1]); | |
71 | exit(2); | |
72 | } | |
73 | bcopy(hp->h_addr, &name.sin_addr, hp->h_length); | |
23237d2e KM |
74 | name.sin_family = AF_INET; |
75 | name.sin_port = htons(atoi(argv[2])); | |
76 | /* Send message. */ | |
a43acb6b | 77 | if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0) |
23237d2e | 78 | perror("sending datagram message"); |
23237d2e KM |
79 | close(sock); |
80 | } |