ConnectedUdpSocket

Struct ConnectedUdpSocket 

Source
pub struct ConnectedUdpSocket { /* private fields */ }
Expand description

A connected User Datagram Protocol socket.

This connected variant of a UdpSocket and can be created by calling connect on a UdpSocket.

Also see UdpSocket for more creating a socket, including setting various options.

§Deregistering

ConnectedUdpSocket will deregister itself when dropped.

§Examples

use mio_st::event::{Events, EventedId};
use mio_st::net::{ConnectedUdpSocket, UdpSocket};
use mio_st::poll::{Interests, PollOption, Poller};

const ECHOER_ID: EventedId = EventedId(0);
const SENDER_ID: EventedId = EventedId(1);

// Create our echoer.
let echoer_addr = "127.0.0.1:7008".parse()?;
let mut echoer = UdpSocket::bind(echoer_addr)?;

// Then we connect to the server.
let sender_addr = "127.0.0.1:7009".parse()?;
let mut sender = ConnectedUdpSocket::connect(sender_addr, echoer_addr)?;

// Create our poll instance and events container.
let mut poller = Poller::new()?;
let mut events = Events::new();

// Register our echoer and sender.
poller.register(&mut echoer, ECHOER_ID, Interests::READABLE, PollOption::Level)?;
poller.register(&mut sender, SENDER_ID, Interests::WRITABLE, PollOption::Level)?;

loop {
    poller.poll(&mut events, None)?;

    for event in &mut events {
        match event.id() {
            ECHOER_ID => {
                let mut buf = [0; 20];
                let (recv_n, address) = echoer.recv_from(&mut buf)?;
                println!("Received: {:?} from {}", &buf[0..recv_n], address);
            },
            SENDER_ID => {
                let msg = b"hello world";
                sender.send(msg)?;
            },
            _ => unreachable!(),
        }
    }
}

Implementations§

Source§

impl ConnectedUdpSocket

Source

pub const INTERESTS: Interests = Interests::BOTH

The interests to use when registering to receive both readable and writable events.

Source

pub fn connect( bind_address: SocketAddr, connect_address: SocketAddr, ) -> Result<ConnectedUdpSocket>

Creates a connected UDP socket.

This method first binds a UDP socket to the bind_address, then connects that socket to connect_address. The is convenience method for a call to UdpSocket::bind followed by a call to connect.

Source

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

Returns the socket address that this socket was created from.

§Examples
use mio_st::net::UdpSocket;

let address = "127.0.0.1:7010".parse()?;
let mut socket = UdpSocket::bind(address)?;

assert_eq!(socket.local_addr()?, address);
Source

pub fn send(&mut self, buf: &[u8]) -> Result<usize>

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

§Examples
use mio_st::net::ConnectedUdpSocket;

let local_addr = "127.0.0.1:7011".parse()?;
let remote_addr = "127.0.0.1:7012".parse()?;
let mut socket = ConnectedUdpSocket::connect(local_addr, remote_addr)?;

// We must check if the socket is writable before calling send, or we
// could run into a WouldBlock error.

let bytes_sent = socket.send(&[9; 9])?;
assert_eq!(bytes_sent, 9);
Source

pub fn recv(&mut self, buf: &mut [u8]) -> Result<usize>

Receives data from the socket. On success, returns the number of bytes read.

§Examples
use mio_st::net::ConnectedUdpSocket;

let local_addr = "127.0.0.1:7013".parse()?;
let remote_addr = "127.0.0.1:7014".parse()?;
let mut socket = ConnectedUdpSocket::connect(local_addr, remote_addr)?;

// We must check if the socket is readable before calling recv, or we
// could run into a WouldBlock error.

let mut buf = [0; 9];
let num_recv = socket.recv(&mut buf)?;
println!("Received {:?} -> {:?} bytes", buf, num_recv);
Source

pub fn peek(&mut self, buf: &mut [u8]) -> Result<usize>

Receives data from the socket, without removing it from the input queue. On success, returns the number of bytes read.

§Examples
use mio_st::net::ConnectedUdpSocket;

let local_addr = "127.0.0.1:7015".parse()?;
let remote_addr = "127.0.0.1:7016".parse()?;
let mut socket = ConnectedUdpSocket::connect(local_addr, remote_addr)?;

// We must check if the socket is readable before calling peek, or we
// could run into a WouldBlock error.

let mut buf1 = [0; 9];
let mut buf2 = [0; 9];
let num_recv1 = socket.peek(&mut buf1)?;
let num_recv2 = socket.recv(&mut buf2)?;
assert_eq!(buf1, buf2);
assert_eq!(num_recv1, num_recv2);
Source

pub fn take_error(&mut self) -> Result<Option<Error>>

Get the value of the SO_ERROR option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

Trait Implementations§

Source§

impl AsRawFd for ConnectedUdpSocket

Available on Unix only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for ConnectedUdpSocket

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Evented for ConnectedUdpSocket

Source§

fn register( &mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption, ) -> Result<()>

Register self with the given Poller instance. Read more
Source§

fn reregister( &mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption, ) -> Result<()>

Reregister self with the given Poller instance. Read more
Source§

fn deregister(&mut self, poller: &mut Poller) -> Result<()>

Deregister self from the given Poller instance Read more
Source§

impl FromRawFd for ConnectedUdpSocket

Available on Unix only.
Source§

unsafe fn from_raw_fd(fd: RawFd) -> ConnectedUdpSocket

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for ConnectedUdpSocket

Available on Unix only.
Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.