Struct mio_st::net::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, 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§
source§impl ConnectedUdpSocket
impl ConnectedUdpSocket
sourcepub const INTERESTS: Interests = Interests::BOTH
pub const INTERESTS: Interests = Interests::BOTH
The interests to use when registering to receive both readable and writable events.
sourcepub fn connect(
bind_address: SocketAddr,
connect_address: SocketAddr
) -> Result<ConnectedUdpSocket>
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
.
sourcepub fn local_addr(&mut self) -> Result<SocketAddr>
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);
sourcepub fn send(&mut self, buf: &[u8]) -> Result<usize>
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);
sourcepub fn recv(&mut self, buf: &mut [u8]) -> Result<usize>
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);
sourcepub fn peek(&mut self, buf: &mut [u8]) -> Result<usize>
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);
sourcepub fn take_error(&mut self) -> Result<Option<Error>>
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
impl AsRawFd for ConnectedUdpSocket
source§impl Debug for ConnectedUdpSocket
impl Debug for ConnectedUdpSocket
source§impl Evented for ConnectedUdpSocket
impl Evented for ConnectedUdpSocket
source§impl FromRawFd for ConnectedUdpSocket
impl FromRawFd for ConnectedUdpSocket
source§unsafe fn from_raw_fd(fd: RawFd) -> ConnectedUdpSocket
unsafe fn from_raw_fd(fd: RawFd) -> ConnectedUdpSocket
Self
from the given raw file
descriptor. Read more