Erlang logo
Reference Manual
Release Notes
PDF
Top

Kernel
Reference Manual
Version 2.15.1


Expand All
Contract All

Table of Contents

gen_udp

MODULE

gen_udp

MODULE SUMMARY

Interface to UDP sockets

DESCRIPTION

The gen_udp module provides functions for communicating with sockets using the UDP protocol.

DATA TYPES

option() = {active, true | false | once}
         | {add_membership,
            {inet:ip_address(), inet:ip_address()}}
         | {broadcast, boolean()}
         | {buffer, integer() >= 0}
         | {deliver, port | term}
         | {dontroute, boolean()}
         | {drop_membership,
            {inet:ip_address(), inet:ip_address()}}
         | {header, integer() >= 0}
         | {mode, list | binary}
         | list
         | binary
         | {multicast_if, inet:ip_address()}
         | {multicast_loop, boolean()}
         | {multicast_ttl, integer() >= 0}
         | {priority, integer() >= 0}
         | {raw,
            Protocol :: integer() >= 0,
            OptionNum :: integer() >= 0,
            ValueBin :: binary()}
         | {read_packets, integer() >= 0}
         | {recbuf, integer() >= 0}
         | {reuseaddr, boolean()}
         | {sndbuf, integer() >= 0}
         | {tos, integer() >= 0}

option_name() = active
              | broadcast
              | buffer
              | deliver
              | dontroute
              | header
              | mode
              | multicast_if
              | multicast_loop
              | multicast_ttl
              | priority
              | {raw,
                 Protocol :: integer() >= 0,
                 OptionNum :: integer() >= 0,
                 ValueSpec :: (ValueSize :: integer() >= 0)
                            | (ValueBin :: binary())}
              | read_packets
              | recbuf
              | reuseaddr
              | sndbuf
              | tos

socket()

As returned by open/1,2.

EXPORTS

open(Port) -> {ok, Socket} | {error, Reason}
open(Port, Opts) -> {ok, Socket} | {error, Reason}

Types:

Opts = [Option]
Option = {ip, inet:ip_address()}
       | {fd, integer() >= 0}
       | {ifaddr, inet:ip_address()}
       | inet:address_family()
       | {port, inet:port_number()}
       | option()
Socket = socket()
Reason = inet:posix()

Associates a UDP port number (Port) with the calling process.

The available options are:

list

Received Packet is delivered as a list.

binary

Received Packet is delivered as a binary.

{ip, ip_address()}

If the host has several network interfaces, this option specifies which one to use.

{fd, integer() >= 0}

If a socket has somehow been opened without using gen_udp, use this option to pass the file descriptor for it.

inet6

Set up the socket for IPv6.

inet

Set up the socket for IPv4.

Opt

See inet:setopts/2.

The returned socket Socket is used to send packets from this port with send/4. When UDP packets arrive at the opened port, they are delivered as messages:

{udp, Socket, IP, InPortNo, Packet}

Note that arriving UDP packets that are longer than the receive buffer option specifies, might be truncated without warning.

IP and InPortNo define the address from which Packet came. Packet is a list of bytes if the option list was specified. Packet is a binary if the option binary was specified.

Default value for the receive buffer option is {recbuf, 8192}.

If Port == 0, the underlying OS assigns a free UDP port, use inet:port/1 to retrieve it.

send(Socket, Address, Port, Packet) -> ok | {error, Reason}

Types:

Socket = socket()
Packet = iodata()
Reason = not_owner | inet:posix()

Sends a packet to the specified address and port. The Address argument can be either a hostname, or an IP address.

recv(Socket, Length) ->
        {ok, {Address, Port, Packet}} | {error, Reason}

recv(Socket, Length, Timeout) ->
        {ok, {Address, Port, Packet}} | {error, Reason}

Types:

Socket = socket()
Length = integer() >= 0
Timeout = timeout()
Packet = string() | binary()
Reason = not_owner | inet:posix()

This function receives a packet from a socket in passive mode.

The optional Timeout parameter specifies a timeout in milliseconds. The default value is infinity.

controlling_process(Socket, Pid) -> ok

Types:

Socket = socket()
Pid = pid()

Assigns a new controlling process Pid to Socket. The controlling process is the process which receives messages from the socket.

close(Socket) -> ok

Types:

Socket = socket()

Closes a UDP socket.