class UDPSocket < IO

A UDP socket, sending and receiving datagrams.

server = UDPSocket.new
server.bind("127.0.0.1", 9000)
client = UDPSocket.new
client.send("ping", "127.0.0.1", 9000)
data, from = server.recvfrom(512)   # "ping" and the client's address

<std:socket>:51

Class methods

def self.new -> UDPSocket

A socket bound to a free port of every IPv4 address; #bind binds it elsewhere.

<std:socket>:54

Methods

def bind(host: String, port: Int) -> Void

Binds the socket to port of host (port 0: a free one), replacing its address. Raises SystemCallError when the address is taken.

<std:socket>:57

def connect(host: String, port: Int) -> Void

Sets the default destination of #send without a host, and receives only from it.

<std:socket>:60

def send(data: String, host: String, port: Int) -> Int

Sends data as one datagram to host at port; the number of bytes sent.

<std:socket>:63

def send(data: Bytes, host: String, port: Int) -> Int

Sends data as one datagram to host at port; the number of bytes sent.

<std:socket>:65

def send(data: String) -> Int

Sends data as one datagram to the address given to #connect; the number of bytes sent.

<std:socket>:68

def send(data: Bytes) -> Int

Sends data as one datagram to the address given to #connect; the number of bytes sent.

<std:socket>:70

def recv(length: Int) -> String

The next datagram, waiting for one, as a string (invalid UTF-8 replaced). At most length bytes are read: the rest of a longer datagram is lost.

<std:socket>:74

def recv_bytes(length: Int) -> Bytes

The next datagram as Bytes, as #recv reads it.

<std:socket>:76

def recvfrom(length: Int) -> [String, Socket::Address]

The next datagram, as #recv reads it, and the sender's address.

<std:socket>:78

def recvfrom_bytes(length: Int) -> [Bytes, Socket::Address]

The next datagram as Bytes and the sender's address.

<std:socket>:80

def local_address -> Socket::Address

The address and port the socket is bound to.

<std:socket>:82