Struct std::net::UdpSocket [] [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.

Examples

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>(addr: A) -> Result<UdpSocket>

Creates a UDP socket from the given address.

The 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>(&self, buf: &[u8], addr: A) -> Result<usize>

Sends data on the socket to the given address. On success, returns the number of bytes written.

Address type can be any implementor of ToSocketAddrs trait. See its documentation for concrete examples.

fn local_addr(&self) -> Result<SocketAddr>

Returns the socket address that this socket was created from.

fn try_clone(&self) -> Result<UdpSocket>

Creates 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<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Sets the broadcast flag on or off.

fn set_multicast_loop(&self, on: bool) -> Result<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Sets 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<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Joins a multicast IP address (becomes a member of it).

fn leave_multicast(&self, multi: &IpAddr) -> Result<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Leaves a multicast IP address (drops membership from it).

fn set_multicast_time_to_live(&self, ttl: i32) -> Result<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Sets the multicast TTL.

fn set_time_to_live(&self, ttl: i32) -> Result<()>

Unstable

: remaining functions have not been scrutinized enough to be stabilized

Sets this socket's TTL.

Trait Implementations

impl Debug for UdpSocket

fn fmt(&self, f: &mut Formatter) -> Result

impl AsRawFd for UdpSocket

fn as_raw_fd(&self) -> RawFd

impl FromRawFd for UdpSocket

unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket

impl AsRawFd for UdpSocket

fn as_raw_fd(&self) -> RawFd

impl FromRawFd for UdpSocket

unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket