Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | .\" Copyright (c) 1986 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
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. | |
31 | .\" | |
78ed81a3 | 32 | .\" from: @(#)dgramread.c 6.4 (Berkeley) 4/17/91 |
33 | .\" dgramread.c,v 1.2 1993/08/01 18:23:31 mycroft Exp | |
15637ed4 RG |
34 | .\" |
35 | #include <sys/types.h> | |
36 | #include <sys/socket.h> | |
37 | #include <netinet/in.h> | |
38 | #include <stdio.h> | |
39 | ||
40 | /* | |
41 | * In the included file <netinet/in.h> a sockaddr_in is defined as follows: | |
42 | * struct sockaddr_in { | |
43 | * short sin_family; | |
44 | * u_short sin_port; | |
45 | * struct in_addr sin_addr; | |
46 | * char sin_zero[8]; | |
47 | * }; | |
48 | * | |
49 | * This program creates a datagram socket, binds a name to it, then reads | |
50 | * from the socket. | |
51 | */ | |
52 | main() | |
53 | { | |
54 | int sock, length; | |
55 | struct sockaddr_in name; | |
56 | char buf[1024]; | |
57 | ||
58 | /* Create socket from which to read. */ | |
59 | sock = socket(AF_INET, SOCK_DGRAM, 0); | |
60 | if (sock < 0) { | |
61 | perror("opening datagram socket"); | |
62 | exit(1); | |
63 | } | |
64 | /* Create name with wildcards. */ | |
65 | name.sin_family = AF_INET; | |
66 | name.sin_addr.s_addr = INADDR_ANY; | |
67 | name.sin_port = 0; | |
68 | if (bind(sock, &name, sizeof(name))) { | |
69 | perror("binding datagram socket"); | |
70 | exit(1); | |
71 | } | |
72 | /* Find assigned port value and print it out. */ | |
73 | length = sizeof(name); | |
74 | if (getsockname(sock, &name, &length)) { | |
75 | perror("getting socket name"); | |
76 | exit(1); | |
77 | } | |
78 | printf("Socket has port #%d\en", ntohs(name.sin_port)); | |
79 | /* Read from the socket */ | |
80 | if (read(sock, buf, 1024) < 0) | |
81 | perror("receiving datagram packet"); | |
82 | printf("-->%s\en", buf); | |
83 | close(sock); | |
84 | } |