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, Ready};
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§

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

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.

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);

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);

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);

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);

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§

Extracts the raw file descriptor. Read more
Formats the value using the given formatter. Read more
Register self with the given Poller instance. Read more
Reregister self with the given Poller instance. Read more
Deregister self from the given Poller instance Read more
Constructs a new instance of Self from the given raw file descriptor. Read more
Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.