Struct std::old_io::net::udp::UdpSocketUnstable
[-] [+]
[src]
pub struct UdpSocket { // some fields omitted }
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.
Example
#![allow(unused_must_use)] use std::old_io::net::udp::UdpSocket; use std::old_io::net::ip::{Ipv4Addr, SocketAddr}; fn main() { let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 }; let mut socket = match UdpSocket::bind(addr) { Ok(s) => s, Err(e) => panic!("couldn't bind socket: {}", e), }; let mut buf = [0; 10]; match socket.recv_from(&mut buf) { Ok((amt, src)) => { // Send a reply to the socket we received data from let buf = &mut buf[..amt]; buf.reverse(); socket.send_to(buf, src); } Err(e) => println!("couldn't receive a datagram: {}", e) } drop(socket); // close the socket }use std::old_io::net::udp::UdpSocket; use std::old_io::net::ip::{Ipv4Addr, SocketAddr}; fn main() { let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 }; let mut socket = match UdpSocket::bind(addr) { Ok(s) => s, Err(e) => panic!("couldn't bind socket: {}", e), }; let mut buf = [0; 10]; match socket.recv_from(&mut buf) { Ok((amt, src)) => { // Send a reply to the socket we received data from let buf = &mut buf[..amt]; buf.reverse(); socket.send_to(buf, src); } Err(e) => println!("couldn't receive a datagram: {}", e) } drop(socket); // close the socket }
Methods
impl UdpSocket
fn bind<A: ToSocketAddr>(addr: A) -> IoResult<UdpSocket>
Creates a UDP socket from the given address.
Address type can be any implementor of ToSocketAddr
trait. See its
documentation for concrete examples.
fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(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: ToSocketAddr>(&mut self, buf: &[u8], addr: A) -> IoResult<()>
Sends data on the socket to the given address. Returns nothing on success.
Address type can be any implementer of ToSocketAddr
trait. See its
documentation for concrete examples.
fn socket_name(&mut self) -> IoResult<SocketAddr>
Returns the socket address that this socket was created from.
fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()>
Joins a multicast IP address (becomes a member of it)
fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()>
Leaves a multicast IP address (drops membership from it)
fn set_multicast_loop(&mut self, on: bool) -> IoResult<()>
Set the multicast loop flag to the specified value
This lets multicast packets loop back to local sockets (if enabled)
fn set_multicast_ttl(&mut self, ttl: isize) -> IoResult<()>
Sets the multicast TTL
fn set_ttl(&mut self, ttl: isize) -> IoResult<()>
Sets this socket's TTL
fn set_broadcast(&mut self, broadcast: bool) -> IoResult<()>
Sets the broadcast flag on or off
fn set_timeout(&mut self, timeout_ms: Option<u64>)
Sets the read/write timeout for this socket.
For more information, see TcpStream::set_timeout
fn set_read_timeout(&mut self, timeout_ms: Option<u64>)
Sets the read timeout for this socket.
For more information, see TcpStream::set_timeout
fn set_write_timeout(&mut self, timeout_ms: Option<u64>)
Sets the write timeout for this socket.
For more information, see TcpStream::set_timeout
Trait Implementations
impl Clone for UdpSocket
fn clone(&self) -> UdpSocket
Creates a new handle to this UDP socket, allowing for simultaneous reads and writes of the socket.
The underlying UDP socket will not be closed until all handles to the socket have been deallocated. Two concurrent reads will not receive the same data. Instead, the first read will receive the first packet received, and the second read will receive the second packet.