Struct std::net::UdpSocketUnstable
[-] [+]
[src]
pub struct UdpSocket(_);
A User Datagram Protocol socket.
This is an implementation of a bound UDP socket. This supports both IPv4 and IPv6 addresses, and there is no corresponding notion of a server because UDP is a datagram protocol.
Example
fn main() { use std::net::UdpSocket; fn foo() -> std::io::Result<()> { let mut socket = try!(UdpSocket::bind("127.0.0.1:34254")); let mut buf = [0; 10]; let (amt, src) = try!(socket.recv_from(&mut buf)); // Send a reply to the socket we received data from let buf = &mut buf[..amt]; buf.reverse(); try!(socket.send_to(buf, &src)); drop(socket); // close the socket Ok(()) } }use std::net::UdpSocket; let mut socket = try!(UdpSocket::bind("127.0.0.1:34254")); let mut buf = [0; 10]; let (amt, src) = try!(socket.recv_from(&mut buf)); // Send a reply to the socket we received data from let buf = &mut buf[..amt]; buf.reverse(); try!(socket.send_to(buf, &src)); drop(socket); // close the socket
Methods
impl UdpSocket
fn bind<A: ToSocketAddrs + ?Sized>(addr: &A) -> Result<UdpSocket>
Creates a UDP socket from the given address.
Address type can be any implementor of ToSocketAddr
trait. See its
documentation for concrete examples.
fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives data from the socket. On success, returns the number of bytes read and the address from whence the data came.
fn send_to<A: ToSocketAddrs + ?Sized>(&self, buf: &[u8], addr: &A) -> Result<usize>
Sends data on the socket to the given address. Returns nothing on success.
Address type can be any implementor of ToSocketAddrs
trait. See its
documentation for concrete examples.
fn socket_addr(&self) -> Result<SocketAddr>
Returns the socket address that this socket was created from.
fn try_clone(&self) -> Result<UdpSocket>
Create a new independently owned handle to the underlying socket.
The returned UdpSocket
is a reference to the same socket that this
object references. Both handles will read and write the same port, and
options set on one socket will be propagated to the other.
fn set_broadcast(&self, on: bool) -> Result<()>
Sets the broadcast flag on or off
fn set_multicast_loop(&self, on: bool) -> Result<()>
Set the multicast loop flag to the specified value
This lets multicast packets loop back to local sockets (if enabled)
fn join_multicast(&self, multi: &IpAddr) -> Result<()>
Joins a multicast IP address (becomes a member of it)
fn leave_multicast(&self, multi: &IpAddr) -> Result<()>
Leaves a multicast IP address (drops membership from it)
fn set_multicast_time_to_live(&self, ttl: i32) -> Result<()>
Sets the multicast TTL
fn set_time_to_live(&self, ttl: i32) -> Result<()>
Sets this socket's TTL