File: gen_udp.3

package info (click to toggle)
erlang-manpages 1%3A12.b.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,188 kB
  • ctags: 2
  • sloc: makefile: 68; perl: 30; sh: 15
file content (176 lines) | stat: -rw-r--r-- 3,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
.TH gen_udp 3 "kernel  2.12.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
gen_udp \- Interface to UDP sockets
.SH DESCRIPTION
.LP
The \fIgen_udp\fR module provides functions for communicating with sockets using the UDP protocol\&.

.SH DATA TYPES

.nf
ip_address()
  see inet(3)

posix()
  see inet(3)

socket()
  as returned by open/1,2
.fi
.SH EXPORTS
.LP
.B
open(Port) -> {ok, Socket} | {error, Reason}
.br
.B
open(Port, Options) -> {ok, Socket} | {error, Reason}
.br
.RS
.TP
Types
Port = 0\&.\&.65535
.br
Options = [Opt]
.br
Opt -- see below
.br
Socket = socket()
.br
Reason = posix()
.br
.RE
.RS
.LP
Associates a UDP port number (\fIPort\fR) with the calling process\&.
.LP
The available options are:
.RS 2
.TP 4
.B
\fIlist\fR:
Received \fIPacket\fR is delivered as a list\&.
.TP 4
.B
\fIbinary\fR:
Received \fIPacket\fR is delivered as a binary\&.
.TP 4
.B
\fI{ip, ip_address()}\fR:
If the host has several network interfaces, this option specifies which one to use\&.
.TP 4
.B
\fI{fd, int()}\fR:
If a socket has somehow been opened without using \fIgen_udp\fR, use this option to pass the file descriptor for it\&.
.TP 4
.B
\fIinet6\fR:
Set up the socket for IPv6\&.
.TP 4
.B
\fIinet\fR:
Set up the socket for IPv4\&.
.TP 4
.B
\fIOpt\fR:
See inet:setopts/2\&.
.RE
.LP
The returned socket \fISocket\fR is used to send packets from this port with \fIsend/4\fR\&. When UDP packets arrive at the opened port, they are delivered as messages:

.nf
{udp, Socket, IP, InPortNo, Packet}
.fi
.LP
Note that arriving UDP packets that are longer than the receive buffer option specifies, might be truncated without warning\&.
.LP
\fIIP\fR and \fIInPortNo\fR define the address from which \fIPacket\fR came\&. \fIPacket\fR is a list of bytes if the option \fIlist\fR was specified\&. \fIPacket\fR is a binary if the option \fIbinary\fR was specified\&.
.LP
Default value for the receive buffer option is \fI{recbuf, 8192}\fR\&.
.LP
If \fIPort == 0\fR, the underlying OS assigns a free UDP port, use \fIinet:port/1\fR to retrieve it\&.
.RE
.LP
.B
send(Socket, Address, Port, Packet) -> ok | {error, Reason}
.br
.RS
.TP
Types
Socket = socket()
.br
Address = string() | atom() | ip_address()
.br
Port = 0\&.\&.65535
.br
Packet = [char()] | binary()
.br
Reason = not_owner | posix()
.br
.RE
.RS
.LP
Sends a packet to the specified address and port\&. The \fIAddress\fR argument can be either a hostname, or an IP address\&.
.RE
.LP
.B
recv(Socket, Length) -> {ok, {Address, Port, Packet}} | {error, Reason}
.br
.B
recv(Socket, Length, Timeout) -> {ok, {Address, Port, Packet}} | {error, Reason}
.br
.RS
.TP
Types
Socket = socket()
.br
Length = int()
.br
Address = ip_address()
.br
Port = 0\&.\&.65535
.br
Packet = [char()] | binary()
.br
Timeout = int() | infinity
.br
Reason = not_owner | posix()
.br
.RE
.RS
.LP
This function receives a packet from a socket in passive mode\&.
.LP
The optional \fITimeout\fR parameter specifies a timeout in milliseconds\&. The default value is \fIinfinity\fR\&.
.RE
.LP
.B
controlling_process(Socket, Pid) -> ok
.br
.RS
.TP
Types
Socket = socket()
.br
Pid = pid()
.br
.RE
.RS
.LP
Assigns a new controlling process \fIPid\fR to \fISocket\fR\&. The controlling process is the process which receives messages from the socket\&.
.RE
.LP
.B
close(Socket) -> ok | {error, Reason}
.br
.RS
.TP
Types
Socket = socket()
.br
Reason = not_owner | posix()
.br
.RE
.RS
.LP
Closes a UDP socket\&.
.RE