A UDP socket
Example
This example sends a DNS query to CloudFlare's DNS server and prints the response metadata.
use std::net::{SocketAddr, UdpSocket};
use std::io::Result;
fn dns_query() -> Result<()> {
let request = std::concat!(
"\x00\x01", // Transaction id
"\x01\x20", // Flags (standard query)
"\x00\x01", // Question RRs
"\x00\x00", // Answer RRs
"\x00\x00", // Authority RRs
"\x00\x00", // Additional RRs
"\x04docs", // Domain name
"\x0calumina-lang",
"\x03net",
"\0",
"\x00\x1c", // Type (AAAA)
"\x00\x01" // Class (IN)
);
// Bind on a random local port
let local_addr = SocketAddr::parse("[::]:0").unwrap();
// Cloudflare DNS resolver
let remote_addr = SocketAddr::parse("[2606:4700:4700::1111]:53").unwrap();
let socket = UdpSocket::bind(&local_addr)?;
defer socket.close();
socket.send_to(request, &remote_addr)?;
let buf: [u8; 65536];
let (size, src) = socket.recv_from(&buf)?;
println!("Received {} bytes from {}", size, src);
println!(" {} questions", (&buf[4] as &u16).from_be());
println!(" {} answers", (&buf[6] as &u16).from_be());
println!(" {} authorities", (&buf[8] as &u16).from_be());
println!(" {} additional", (&buf[10] as &u16).from_be());
Result::ok(())
}
dns_query().unwrap();
Run this example
Fields
-
socket: Socket
Methods
impl UdpSocket { ... }
-
Create a new UDP socket from a raw socket.
-
fn bind(addr: &SocketAddr) -> Result<UdpSocket>
Creates a new UDP socket bound to the specified address.
Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); let buf: [u8; 1024]; let (size, peer) = socket.recv_from(&buf).unwrap(); println!("Received {} bytes from {}", size, peer);Run this example -
fn connect(self: &UdpSocket, addr: &SocketAddr) -> Result<()>
-
fn recv_from(self: &UdpSocket, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives data from the socket.
Returns the number of bytes read and the address from which the data was received.
-
fn peek_from(self: &UdpSocket, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives data from the socket without consuming it.
Subsequent calls to recv will return the same data.
-
fn send_to(self: &UdpSocket, buf: &[u8], addr: &SocketAddr) -> Result<usize>
Sends data to a particular address.
-
Receives data from the socket.
-
Receives data from the socket without consuming it.
-
Sends data to the socket.
-
fn as_fd(self: &UdpSocket) -> FileDescriptor
-
fn peer_addr(self: &UdpSocket) -> Result<SocketAddr>
Returns the address of the remote peer
-
fn socket_addr(self: &UdpSocket) -> Result<SocketAddr>
Returns the address of the remote peer
-
Sets the time-to-live (TTL) value for the socket.
This value controls the number of hops (routers) that packets can traverse before being discarded. Works for both IPv4 and IPv6 sockets.
Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); socket.set_ttl(64).unwrap(); assert_eq!(socket.ttl().unwrap(), 64);Run this example -
Gets the time-to-live (TTL) value for the socket.
Works for both IPv4 and IPv6 sockets.
Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); let ttl = socket.ttl().unwrap(); println!("TTL: {}", ttl);Run this example -
Sets the read timeout for the socket.
If the provided duration is
Option::none(), then read operations will block indefinitely. Otherwise, read operations will time out after the specified duration.Example
use std::net::{UdpSocket, SocketAddr}; use std::time::Duration; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); socket.set_read_timeout(Option::some(Duration::from_secs(5))).unwrap();Run this example -
Gets the read timeout for the socket.
Returns
Option::none()if reads will block indefinitely.Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); let timeout = socket.read_timeout().unwrap(); if timeout.is_some() { println!("Read timeout: {} seconds", timeout.unwrap().total_secs()); }Run this example -
Sets the write timeout for the socket.
If the provided duration is
Option::none(), then write operations will block indefinitely. Otherwise, write operations will time out after the specified duration.Example
use std::net::{UdpSocket, SocketAddr}; use std::time::Duration; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); socket.set_write_timeout(Option::some(Duration::from_secs(5))).unwrap();Run this example -
Gets the write timeout for the socket.
Returns
Option::none()if writes will block indefinitely.Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); let timeout = socket.write_timeout().unwrap(); if timeout.is_some() { println!("Write timeout: {} seconds", timeout.unwrap().total_secs()); }Run this example -
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, read and write operations will return immediately with an error if they would block.
Example
use std::net::{UdpSocket, SocketAddr}; let addr = SocketAddr::parse("[::]:9000").unwrap(); let socket = UdpSocket::bind(&addr).unwrap(); defer socket.close(); socket.set_nonblocking(true).unwrap();Run this example -
Closes the socket.