1gen_udp(3) Erlang Module Definition gen_udp(3)
2
3
4
6 gen_udp - Interface to UDP sockets.
7
9 This module provides functions for communicating with sockets using the
10 UDP protocol.
11
13 option() =
14 {active, true | false | once | -32768..32767} |
15 {add_membership, {inet:ip_address(), inet:ip_address()}} |
16 {broadcast, boolean()} |
17 {buffer, integer() >= 0} |
18 {deliver, port | term} |
19 {dontroute, boolean()} |
20 {drop_membership, {inet:ip_address(), inet:ip_address()}} |
21 {header, integer() >= 0} |
22 {high_msgq_watermark, integer() >= 1} |
23 {low_msgq_watermark, integer() >= 1} |
24 {mode, list | binary} |
25 list |
26 binary |
27 {multicast_if, inet:ip_address()} |
28 {multicast_loop, boolean()} |
29 {multicast_ttl, integer() >= 0} |
30 {priority, integer() >= 0} |
31 {raw,
32 Protocol :: integer() >= 0,
33 OptionNum :: integer() >= 0,
34 ValueBin :: binary()} |
35 {read_packets, integer() >= 0} |
36 {recbuf, integer() >= 0} |
37 {reuseaddr, boolean()} |
38 {sndbuf, integer() >= 0} |
39 {tos, integer() >= 0} |
40 {ipv6_v6only, boolean()}
41
42 option_name() =
43 active |
44 broadcast |
45 buffer |
46 deliver |
47 dontroute |
48 header |
49 high_msgq_watermark |
50 low_msgq_watermark |
51 mode |
52 multicast_if |
53 multicast_loop |
54 multicast_ttl |
55 priority |
56 {raw,
57 Protocol :: integer() >= 0,
58 OptionNum :: integer() >= 0,
59 ValueSpec ::
60 (ValueSize :: integer() >= 0) | (ValueBin :: binary())} |
61 read_packets |
62 recbuf |
63 reuseaddr |
64 sndbuf |
65 tos |
66 ipv6_v6only
67
68 socket()
69
70 As returned by open/1,2.
71
73 close(Socket) -> ok
74
75 Types:
76
77 Socket = socket()
78
79 Closes a UDP socket.
80
81 controlling_process(Socket, Pid) -> ok | {error, Reason}
82
83 Types:
84
85 Socket = socket()
86 Pid = pid()
87 Reason = closed | not_owner | badarg | inet:posix()
88
89 Assigns a new controlling process Pid to Socket. The controlling
90 process is the process that receives messages from the socket.
91 If called by any other process than the current controlling
92 process, {error, not_owner} is returned. If the process identi‐
93 fied by Pid is not an existing local pid, {error, badarg} is
94 returned. {error, badarg} may also be returned in some cases
95 when Socket is closed during the execution of this function.
96
97 open(Port) -> {ok, Socket} | {error, Reason}
98
99 open(Port, Opts) -> {ok, Socket} | {error, Reason}
100
101 Types:
102
103 Port = inet:port_number()
104 Opts = [Option]
105 Option =
106 {ip, inet:socket_address()} |
107 {fd, integer() >= 0} |
108 {ifaddr, inet:socket_address()} |
109 inet:address_family() |
110 {port, inet:port_number()} |
111 option()
112 Socket = socket()
113 Reason = inet:posix()
114
115 Associates a UDP port number (Port) with the calling process.
116
117 The following options are available:
118
119 list:
120 Received Packet is delivered as a list.
121
122 binary:
123 Received Packet is delivered as a binary.
124
125 {ip, Address}:
126 If the host has many network interfaces, this option speci‐
127 fies which one to use.
128
129 {ifaddr, Address}:
130 Same as {ip, Address}. If the host has many network inter‐
131 faces, this option specifies which one to use.
132
133 {fd, integer() >= 0}:
134 If a socket has somehow been opened without using gen_udp,
135 use this option to pass the file descriptor for it. If Port
136 is not set to 0 and/or {ip, ip_address()} is combined with
137 this option, the fd is bound to the specified interface and
138 port after it is being opened. If these options are not
139 specified, it is assumed that the fd is already bound appro‐
140 priately.
141
142 inet6:
143 Sets up the socket for IPv6.
144
145 inet:
146 Sets up the socket for IPv4.
147
148 local:
149 Sets up a Unix Domain Socket. See inet:local_address()
150
151 {udp_module, module()}:
152 Overrides which callback module is used. Defaults to
153 inet_udp for IPv4 and inet6_udp for IPv6.
154
155 {multicast_if, Address}:
156 Sets the local device for a multicast socket.
157
158 {multicast_loop, true | false}:
159 When true, sent multicast packets are looped back to the
160 local sockets.
161
162 {multicast_ttl, Integer}:
163 Option multicast_ttl changes the time-to-live (TTL) for out‐
164 going multicast datagrams to control the scope of the multi‐
165 casts.
166
167 Datagrams with a TTL of 1 are not forwarded beyond the local
168 network. Defaults to 1.
169
170 {add_membership, {MultiAddress, InterfaceAddress}}:
171 Joins a multicast group.
172
173 {drop_membership, {MultiAddress, InterfaceAddress}}:
174 Leaves a multicast group.
175
176 Opt:
177 See inet:setopts/2.
178
179 The returned socket Socket is used to send packets from this
180 port with send/4. When UDP packets arrive at the opened port, if
181 the socket is in an active mode, the packets are delivered as
182 messages to the controlling process:
183
184 {udp, Socket, IP, InPortNo, Packet}
185
186 If the socket is not in an active mode, data can be retrieved
187 through the recv/2,3 calls. Notice that arriving UDP packets
188 that are longer than the receive buffer option specifies can be
189 truncated without warning.
190
191 When a socket in {active, N} mode (see inet:setopts/2 for
192 details), transitions to passive ({active, false}) mode, the
193 controlling process is notified by a message of the following
194 form:
195
196 {udp_passive, Socket}
197
198 IP and InPortNo define the address from which Packet comes.
199 Packet is a list of bytes if option list is specified. Packet is
200 a binary if option binary is specified.
201
202 Default value for the receive buffer option is {recbuf, 8192}.
203
204 If Port == 0, the underlying OS assigns a free UDP port, use
205 inet:port/1 to retrieve it.
206
207 recv(Socket, Length) ->
208 {ok, {Address, Port, Packet}} | {error, Reason}
209
210 recv(Socket, Length, Timeout) ->
211 {ok, {Address, Port, Packet}} | {error, Reason}
212
213 Types:
214
215 Socket = socket()
216 Length = integer() >= 0
217 Timeout = timeout()
218 Address = inet:ip_address() | inet:returned_non_ip_address()
219 Port = inet:port_number()
220 Packet = string() | binary()
221 Reason = not_owner | inet:posix()
222
223 Receives a packet from a socket in passive mode. Optional param‐
224 eter Timeout specifies a time-out in milliseconds. Defaults to
225 infinity.
226
227 send(Socket, Address, Port, Packet) -> ok | {error, Reason}
228
229 Types:
230
231 Socket = socket()
232 Address = inet:socket_address() | inet:hostname()
233 Port = inet:port_number()
234 Packet = iodata()
235 Reason = not_owner | inet:posix()
236
237 Sends a packet to the specified address and port. Argument
238 Address can be a hostname or a socket address.
239
240
241
242Ericsson AB kernel 5.4.3.2 gen_udp(3)