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_read_timeout(&self, dur: Option<Duration>) -> Result<()>

Sets the read timeout to the timeout specified.

If the value specified is None, then read calls will block indefinitely. It is an error to pass the zero Duration to this method.

Note

Platforms may return a different error code whenever a read times out as a result of setting this option. For example Unix typically returns an error of the kind WouldBlock, but Windows may return TimedOut.

fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()>

Sets the write timeout to the timeout specified.

If the value specified is None, then write calls will block indefinitely. It is an error to pass the zero Duration to this method.

Note

Platforms may return a different error code whenever a write times out as a result of setting this option. For example Unix typically returns an error of the kind WouldBlock, but Windows may return TimedOut.

fn read_timeout(&self) -> Result<Option<Duration>>

Returns the read timeout of this socket.

If the timeout is None, then read calls will block indefinitely.

fn write_timeout(&self) -> Result<Option<Duration>>

Returns the write timeout of this socket.

If the timeout is None, then write calls will block indefinitely.

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 IntoRawFd for UdpSocket

fn into_raw_fd(self) -> RawFd

impl AsRawFd for UdpSocket

fn as_raw_fd(&self) -> RawFd

impl FromRawFd for UdpSocket

unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket

impl IntoRawFd for UdpSocket

fn into_raw_fd(self) -> RawFd