Struct bitflags::__core::net::UdpSocket [] [src]

pub struct UdpSocket(_);
1.0.0

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

use std::net::UdpSocket;

{
    let mut socket = try!(UdpSocket::bind("127.0.0.1:34254"));

    // read from the socket
    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));
} // the socket is closed here

Methods

impl UdpSocket

fn bind<A>(addr: A) -> Result<UdpSocket, Error> where A: ToSocketAddrs

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), Error>

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

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, Error>

Returns the socket address that this socket was created from.

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

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.

1.4.0fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>

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.

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

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.

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

Returns the read timeout of this socket.

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

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

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<(), Error>

impl AsRawFd for UdpSocket

fn as_raw_fd(&self) -> i32

impl FromRawFd for UdpSocket1.1.0

unsafe fn from_raw_fd(fd: i32) -> UdpSocket

impl IntoRawFd for UdpSocket1.4.0

fn into_raw_fd(self) -> i32