| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A UDP socket."><meta name="keywords" content="rust, rustlang, rust-lang, UdpSocket"><title>UdpSocket in tokio::net - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">UdpSocket</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block"><li><a href="#method.async_io">async_io</a></li><li><a href="#method.bind">bind</a></li><li><a href="#method.bind_device">bind_device</a></li><li><a href="#method.broadcast">broadcast</a></li><li><a href="#method.connect">connect</a></li><li><a href="#method.device">device</a></li><li><a href="#method.from_std">from_std</a></li><li><a href="#method.into_std">into_std</a></li><li><a href="#method.join_multicast_v4">join_multicast_v4</a></li><li><a href="#method.join_multicast_v6">join_multicast_v6</a></li><li><a href="#method.leave_multicast_v4">leave_multicast_v4</a></li><li><a href="#method.leave_multicast_v6">leave_multicast_v6</a></li><li><a href="#method.local_addr">local_addr</a></li><li><a href="#method.multicast_loop_v4">multicast_loop_v4</a></li><li><a href="#method.multicast_loop_v6">multicast_loop_v6</a></li><li><a href="#method.multicast_ttl_v4">multicast_ttl_v4</a></li><li><a href="#method.peek_from">peek_from</a></li><li><a href="#method.peek_sender">peek_sender</a></li><li><a href="#method.peer_addr">peer_addr</a></li><li><a href="#method.poll_peek_from">poll_peek_from</a></li><li><a href="#method.poll_peek_sender">poll_peek_sender</a></li><li><a href="#method.poll_recv">poll_recv</a></li><li><a href="#method.poll_recv_from">poll_recv_from</a></li><li><a href="#method.poll_recv_ready">poll_recv_ready</a></li><li><a href="#method.poll_send">poll_send</a></li><li><a href="#method.poll_send_ready">poll_send_ready</a></li><li><a href="#method.poll_send_to">poll_send_to</a></li><li><a href="#method.readable">readable</a></li><li><a href="#method.ready">ready</a></li><li><a href="#method.recv">recv</a></li><li><a href="#method.recv_buf">recv_buf</a></li><li><a href="#method.recv_buf_from">recv_buf_from</a></li><li><a href="#method.recv_from">recv_from</a></li><li><a href="#method.send">send</a></li><li><a href="#method.send_to">send_to</a></li><li><a href="#method.set_broadcast">set_broadcast</a></li><li><a href="#method.set_multicast_loop_v4">set_multicast_loop_v4</a></li><li><a href="#method.set_multicast_loop_v6">set_multicast_loop_v6</a></li><li><a href="#method.set_multicast_ttl_v4">set_multicast_ttl_v4</a></li><li><a href="#method.set_tos">set_tos</a></li><li><a href="#method.set_ttl">set_ttl</a></li><li><a href="#method.take_error">take_error</a></li><li><a href="#method.tos">tos</a></li><li><a href="#method.try_io">try_io</a></li><li><a href="#method.try_peek_from">try_peek_from</a></li><li><a href="#method.try_peek_sender">try_peek_sender</a></li><li><a href="#method.try_recv">try_recv</a></li><li><a href="#method.try_recv_buf">try_recv_buf</a></li><li><a href="#method.try_recv_buf_from">try_recv_buf_from</a></li><li><a href="#method.try_recv_from">try_recv_from</a></li><li><a href="#method.try_send">try_send</a></li><li><a href="#method.try_send_to">try_send_to</a></li><li><a href="#method.ttl">ttl</a></li><li><a href="#method.writable">writable</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block"><li><a href="#impl-AsFd-for-UdpSocket">AsFd</a></li><li><a href="#impl-AsRawFd-for-UdpSocket">AsRawFd</a></li><li><a href="#impl-Debug-for-UdpSocket">Debug</a></li><li><a href="#impl-TryFrom%3CUdpSocket%3E-for-UdpSocket">TryFrom<UdpSocket></a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-RefUnwindSafe-for-UdpSocket">RefUnwindSafe</a></li><li><a href="#impl-Send-for-UdpSocket">Send</a></li><li><a href="#impl-Sync-for-UdpSocket">Sync</a></li><li><a href="#impl-Unpin-for-UdpSocket">Unpin</a></li><li><a href="#impl-UnwindSafe-for-UdpSocket">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-UdpSocket">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-UdpSocket">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-UdpSocket">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-UdpSocket">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-UdpSocket">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-UdpSocket">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-UdpSocket">TryInto<U></a></li></ul></section><h2><a href="index.html">In tokio::net</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Struct <a href="../index.html">tokio</a>::<wbr><a href="index.html">net</a>::<wbr><a class="struct" href="#">UdpSocket</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/tokio/net/udp.rs.html#121-123">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust struct"><code>pub struct UdpSocket { /* private fields */ }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A UDP socket.</p> |
| <p>UDP is “connectionless”, unlike TCP. Meaning, regardless of what address you’ve bound to, a <code>UdpSocket</code> |
| is free to communicate with many different remotes. In tokio there are basically two main ways to use <code>UdpSocket</code>:</p> |
| <ul> |
| <li>one to many: <a href="struct.UdpSocket.html#method.bind"><code>bind</code></a> and use <a href="struct.UdpSocket.html#method.send_to"><code>send_to</code></a> |
| and <a href="struct.UdpSocket.html#method.recv_from"><code>recv_from</code></a> to communicate with many different addresses</li> |
| <li>one to one: <a href="struct.UdpSocket.html#method.connect"><code>connect</code></a> and associate with a single address, using <a href="struct.UdpSocket.html#method.send"><code>send</code></a> |
| and <a href="struct.UdpSocket.html#method.recv"><code>recv</code></a> to communicate only with that remote address</li> |
| </ul> |
| <p>This type does not provide a <code>split</code> method, because this functionality |
| can be achieved by instead wrapping the socket in an <a href="https://doc.rust-lang.org/nightly/alloc/sync/struct.Arc.html"><code>Arc</code></a>. Note that |
| you do not need a <code>Mutex</code> to share the <code>UdpSocket</code> — an <code>Arc<UdpSocket></code> |
| is enough. This is because all of the methods take <code>&self</code> instead of |
| <code>&mut self</code>. Once you have wrapped it in an <code>Arc</code>, you can call |
| <code>.clone()</code> on the <code>Arc<UdpSocket></code> to get multiple shared handles to the |
| same socket. An example of such usage can be found further down.</p> |
| <h2 id="streams"><a href="#streams">Streams</a></h2> |
| <p>If you need to listen over UDP and produce a <a href="https://docs.rs/futures/0.3/futures/stream/trait.Stream.html"><code>Stream</code></a>, you can look |
| at <a href="https://docs.rs/tokio-util/latest/tokio_util/udp/struct.UdpFramed.html"><code>UdpFramed</code></a>.</p> |
| <h2 id="example-one-to-many-bind"><a href="#example-one-to-many-bind">Example: one to many (bind)</a></h2> |
| <p>Using <code>bind</code> we can create a simple echo server that sends and recv’s with many different clients:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="kw">loop </span>{ |
| <span class="kw">let </span>(len, addr) = sock.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes received from {:?}"</span>, len, addr); |
| |
| <span class="kw">let </span>len = sock.send_to(<span class="kw-2">&</span>buf[..len], addr).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes sent"</span>, len); |
| } |
| }</code></pre></div> |
| <h2 id="example-one-to-one-connect"><a href="#example-one-to-one-connect">Example: one to one (connect)</a></h2> |
| <p>Or using <code>connect</code> we can echo with a single remote address using <code>send</code> and <code>recv</code>:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span>remote_addr = <span class="string">"127.0.0.1:59611"</span>; |
| sock.connect(remote_addr).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="kw">loop </span>{ |
| <span class="kw">let </span>len = sock.recv(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes received from {:?}"</span>, len, remote_addr); |
| |
| <span class="kw">let </span>len = sock.send(<span class="kw-2">&</span>buf[..len]).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes sent"</span>, len); |
| } |
| }</code></pre></div> |
| <h2 id="example-splitting-with-arc"><a href="#example-splitting-with-arc">Example: Splitting with <code>Arc</code></a></h2> |
| <p>Because <code>send_to</code> and <code>recv_from</code> take <code>&self</code>. It’s perfectly alright |
| to use an <code>Arc<UdpSocket></code> and share the references to multiple tasks. |
| Here is a similar “echo” example that supports concurrent |
| sending/receiving:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::{net::UdpSocket, sync::mpsc}; |
| <span class="kw">use </span>std::{io, net::SocketAddr, sync::Arc}; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap()).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span>r = Arc::new(sock); |
| <span class="kw">let </span>s = r.clone(); |
| <span class="kw">let </span>(tx, <span class="kw-2">mut </span>rx) = mpsc::channel::<(Vec<u8>, SocketAddr)>(<span class="number">1_000</span>); |
| |
| tokio::spawn(<span class="kw">async move </span>{ |
| <span class="kw">while let </span><span class="prelude-val">Some</span>((bytes, addr)) = rx.recv().<span class="kw">await </span>{ |
| <span class="kw">let </span>len = s.send_to(<span class="kw-2">&</span>bytes, <span class="kw-2">&</span>addr).<span class="kw">await</span>.unwrap(); |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes sent"</span>, len); |
| } |
| }); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="kw">loop </span>{ |
| <span class="kw">let </span>(len, addr) = r.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">println!</span>(<span class="string">"{:?} bytes received from {:?}"</span>, len, addr); |
| tx.send((buf[..len].to_vec(), addr)).<span class="kw">await</span>.unwrap(); |
| } |
| }</code></pre></div> |
| </div></details><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><div id="implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#126-1993">source</a><a href="#impl-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.bind" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#148-165">source</a><h4 class="code-header">pub async fn <a href="#method.bind" class="fnname">bind</a><A: <a class="trait" href="trait.ToSocketAddrs.html" title="trait tokio::net::ToSocketAddrs">ToSocketAddrs</a>>(addr: A) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a>></h4></section></summary><div class="docblock"><p>This function will create a new UDP socket and attempt to bind it to |
| the <code>addr</code> provided.</p> |
| <p>Binding with a port number of 0 will request that the OS assigns a port |
| to this listener. The port allocated can be queried via the <code>local_addr</code> |
| method.</p> |
| <h5 id="example"><a href="#example">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="comment">// use `sock` |
| </span><span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from_std" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#221-224">source</a><h4 class="code-header">pub fn <a href="#method.from_std" class="fnname">from_std</a>(socket: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html" title="struct std::net::udp::UdpSocket">UdpSocket</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a>></h4></section></summary><div class="docblock"><p>Creates new <code>UdpSocket</code> from a previously bound <code>std::net::UdpSocket</code>.</p> |
| <p>This function is intended to be used to wrap a UDP socket from the |
| standard library in the Tokio equivalent.</p> |
| <p>This can be used in conjunction with socket2’s <code>Socket</code> interface to |
| configure a socket before it’s handed off, such as setting options like |
| <code>reuse_address</code> or binding to multiple addresses.</p> |
| <h5 id="notes"><a href="#notes">Notes</a></h5> |
| <p>The caller is responsible for ensuring that the socket is in |
| non-blocking mode. Otherwise all I/O operations on the socket |
| will block the thread, which will cause unexpected behavior. |
| Non-blocking mode can be set using <a href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html#method.set_nonblocking"><code>set_nonblocking</code></a>.</p> |
| <h5 id="panics"><a href="#panics">Panics</a></h5> |
| <p>This function panics if thread-local runtime is not set.</p> |
| <p>The runtime is usually set implicitly when this function is called |
| from a future driven by a tokio runtime, otherwise runtime can be set |
| explicitly with <a href="../runtime/struct.Runtime.html#method.enter"><code>Runtime::enter</code></a> function.</p> |
| <h5 id="example-1"><a href="#example-1">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>addr = <span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap(); |
| <span class="kw">let </span>std_sock = std::net::UdpSocket::bind(addr)<span class="question-mark">?</span>; |
| std_sock.set_nonblocking(<span class="bool-val">true</span>)<span class="question-mark">?</span>; |
| <span class="kw">let </span>sock = UdpSocket::from_std(std_sock)<span class="question-mark">?</span>; |
| <span class="comment">// use `sock`</span></code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into_std" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#248-266">source</a><h4 class="code-header">pub fn <a href="#method.into_std" class="fnname">into_std</a>(self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html" title="struct std::net::udp::UdpSocket">UdpSocket</a>></h4></section></summary><div class="docblock"><p>Turns a <a href="struct.UdpSocket.html"><code>tokio::net::UdpSocket</code></a> into a <a href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html"><code>std::net::UdpSocket</code></a>.</p> |
| <p>The returned <a href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html"><code>std::net::UdpSocket</code></a> will have nonblocking mode set as |
| <code>true</code>. Use <a href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html#method.set_nonblocking"><code>set_nonblocking</code></a> to change the blocking mode if needed.</p> |
| <h5 id="examples"><a href="#examples">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::error::Error; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="kw">let </span>tokio_socket = tokio::net::UdpSocket::bind(<span class="string">"127.0.0.1:0"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span>std_socket = tokio_socket.into_std()<span class="question-mark">?</span>; |
| std_socket.set_nonblocking(<span class="bool-val">false</span>)<span class="question-mark">?</span>; |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.local_addr" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#289-291">source</a><h4 class="code-header">pub fn <a href="#method.local_addr" class="fnname">local_addr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Returns the local address that this socket is bound to.</p> |
| <h5 id="example-2"><a href="#example-2">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>addr = <span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap(); |
| <span class="kw">let </span>sock = UdpSocket::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="comment">// the address the socket is bound to |
| </span><span class="kw">let </span>local_addr = sock.local_addr()<span class="question-mark">?</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peer_addr" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#311-313">source</a><h4 class="code-header">pub fn <a href="#method.peer_addr" class="fnname">peer_addr</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Returns the socket address of the remote peer this socket was connected to.</p> |
| <h5 id="example-3"><a href="#example-3">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>addr = <span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap(); |
| <span class="kw">let </span>peer = <span class="string">"127.0.0.1:11100"</span>.parse::<SocketAddr>().unwrap(); |
| <span class="kw">let </span>sock = UdpSocket::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>; |
| sock.connect(peer).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(peer, sock.peer_addr()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.connect" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#339-356">source</a><h4 class="code-header">pub async fn <a href="#method.connect" class="fnname">connect</a><A: <a class="trait" href="trait.ToSocketAddrs.html" title="trait tokio::net::ToSocketAddrs">ToSocketAddrs</a>>(&self, addr: A) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Connects the UDP socket setting the default destination for send() and |
| limiting packets that are read via recv from the address specified in |
| <code>addr</code>.</p> |
| <h5 id="example-4"><a href="#example-4">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>.parse::<SocketAddr>().unwrap()).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span>remote_addr = <span class="string">"127.0.0.1:59600"</span>.parse::<SocketAddr>().unwrap(); |
| sock.connect(remote_addr).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0u8</span>; <span class="number">32</span>]; |
| <span class="comment">// recv from remote_addr |
| </span><span class="kw">let </span>len = sock.recv(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="comment">// send to remote_addr |
| </span><span class="kw">let </span>_len = sock.send(<span class="kw-2">&</span>buf[..len]).<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#426-429">source</a><h4 class="code-header">pub async fn <a href="#method.ready" class="fnname">ready</a>(&self, interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="struct" href="../io/struct.Ready.html" title="struct tokio::io::Ready">Ready</a>></h4></section></summary><div class="docblock"><p>Waits for any of the requested ready states.</p> |
| <p>This function is usually paired with <code>try_recv()</code> or <code>try_send()</code>. It |
| can be used to concurrently recv / send to the same socket on a single |
| task without splitting the socket.</p> |
| <p>The function may complete without the socket being ready. This is a |
| false-positive and attempting an operation will return with |
| <code>io::ErrorKind::WouldBlock</code>. The function can also return with an empty |
| <a href="../io/struct.Ready.html" title="Ready"><code>Ready</code></a> set, so you should always check the returned value and possibly |
| wait again if the requested states are not set.</p> |
| <h5 id="cancel-safety"><a href="#cancel-safety">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to read or write that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-1"><a href="#examples-1">Examples</a></h5> |
| <p>Concurrently receive from and send to the socket on the same task |
| without splitting.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::io::{<span class="self">self</span>, Interest}; |
| <span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="kw">let </span>ready = socket.ready(Interest::READABLE | Interest::WRITABLE).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">if </span>ready.is_readable() { |
| <span class="comment">// The buffer is **not** included in the async task and will only exist |
| // on the stack. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>data = [<span class="number">0</span>; <span class="number">1024</span>]; |
| <span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>data[..]) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"received {:?}"</span>, <span class="kw-2">&</span>data[..n]); |
| } |
| <span class="comment">// False-positive, continue |
| </span><span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => {} |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="kw">if </span>ready.is_writable() { |
| <span class="comment">// Write some data |
| </span><span class="kw">match </span>socket.try_send(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"sent {} bytes"</span>, n); |
| } |
| <span class="comment">// False-positive, continue |
| </span><span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => {} |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| } |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.writable" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#481-484">source</a><h4 class="code-header">pub async fn <a href="#method.writable" class="fnname">writable</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Waits for the socket to become writable.</p> |
| <p>This function is equivalent to <code>ready(Interest::WRITABLE)</code> and is |
| usually paired with <code>try_send()</code> or <code>try_send_to()</code>.</p> |
| <p>The function may complete without the socket being writable. This is a |
| false-positive and attempting a <code>try_send()</code> will return with |
| <code>io::ErrorKind::WouldBlock</code>.</p> |
| <h5 id="cancel-safety-1"><a href="#cancel-safety-1">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to write that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-2"><a href="#examples-2">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Bind socket |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be writable |
| </span>socket.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to send data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_send(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_send_ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#515-517">source</a><h4 class="code-header">pub fn <a href="#method.poll_send_ready" class="fnname">poll_send_ready</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>>></h4></section></summary><div class="docblock"><p>Polls for write/send readiness.</p> |
| <p>If the udp stream is not currently ready for sending, this method will |
| store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the udp |
| stream becomes ready for sending, <code>Waker::wake</code> will be called on the |
| waker.</p> |
| <p>Note that on multiple calls to <code>poll_send_ready</code> or <code>poll_send</code>, only |
| the <code>Waker</code> from the <code>Context</code> passed to the most recent call is |
| scheduled to receive a wakeup. (However, <code>poll_recv_ready</code> retains a |
| second, independent waker.)</p> |
| <p>This function is intended for cases where creating and pinning a future |
| via <a href="struct.UdpSocket.html#method.writable"><code>writable</code></a> is not feasible. Where possible, using <a href="struct.UdpSocket.html#method.writable"><code>writable</code></a> is |
| preferred, as this supports polling from multiple tasks at once.</p> |
| <h5 id="return-value"><a href="#return-value">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the udp stream is not ready for writing.</li> |
| <li><code>Poll::Ready(Ok(()))</code> if the udp stream is ready for writing.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors"><a href="#errors">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.send" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#556-561">source</a><h4 class="code-header">pub async fn <a href="#method.send" class="fnname">send</a>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Sends data on the socket to the remote address that the socket is |
| connected to.</p> |
| <p>The <a href="struct.UdpSocket.html#method.connect"><code>connect</code></a> method will connect this socket to a remote address. |
| This method will fail if the socket is not connected.</p> |
| <h5 id="return"><a href="#return">Return</a></h5> |
| <p>On success, the number of bytes sent is returned, otherwise, the |
| encountered error is returned.</p> |
| <h5 id="cancel-safety-2"><a href="#cancel-safety-2">Cancel safety</a></h5> |
| <p>This method is cancel safe. If <code>send</code> is used as the event in a |
| <a href="../macro.select.html"><code>tokio::select!</code></a> statement and some other branch |
| completes first, then it is guaranteed that the message was not sent.</p> |
| <h5 id="examples-3"><a href="#examples-3">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::io; |
| <span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Bind socket |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Send a message |
| </span>socket.send(<span class="string">b"hello world"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_send" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#586-590">source</a><h4 class="code-header">pub fn <a href="#method.poll_send" class="fnname">poll_send</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>>></h4></section></summary><div class="docblock"><p>Attempts to send data on the socket to the remote address to which it |
| was previously <code>connect</code>ed.</p> |
| <p>The <a href="struct.UdpSocket.html#method.connect"><code>connect</code></a> method will connect this socket to a remote address. |
| This method will fail if the socket is not connected.</p> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the send direction, |
| only the <code>Waker</code> from the <code>Context</code> passed to the most recent call will |
| be scheduled to receive a wakeup.</p> |
| <h5 id="return-value-1"><a href="#return-value-1">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the socket is not available to write</li> |
| <li><code>Poll::Ready(Ok(n))</code> <code>n</code> is the number of bytes sent</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-1"><a href="#errors-1">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_send" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#640-644">source</a><h4 class="code-header">pub fn <a href="#method.try_send" class="fnname">try_send</a>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to send data on the socket to the remote address to which it is |
| connected.</p> |
| <p>When the socket buffer is full, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>writable()</code>.</p> |
| <h5 id="returns"><a href="#returns">Returns</a></h5> |
| <p>If successful, <code>Ok(n)</code> is returned, where <code>n</code> is the number of bytes |
| sent. If the socket is not ready to send data, |
| <code>Err(ErrorKind::WouldBlock)</code> is returned.</p> |
| <h5 id="examples-4"><a href="#examples-4">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Bind a UDP socket |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Connect to a peer |
| </span>socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be writable |
| </span>socket.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// Try to send data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_send(<span class="string">b"hello world"</span>) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.readable" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#701-704">source</a><h4 class="code-header">pub async fn <a href="#method.readable" class="fnname">readable</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Waits for the socket to become readable.</p> |
| <p>This function is equivalent to <code>ready(Interest::READABLE)</code> and is usually |
| paired with <code>try_recv()</code>.</p> |
| <p>The function may complete without the socket being readable. This is a |
| false-positive and attempting a <code>try_recv()</code> will return with |
| <code>io::ErrorKind::WouldBlock</code>.</p> |
| <h5 id="cancel-safety-3"><a href="#cancel-safety-3">Cancel safety</a></h5> |
| <p>This method is cancel safe. Once a readiness event occurs, the method |
| will continue to return immediately until the readiness event is |
| consumed by an attempt to read that fails with <code>WouldBlock</code> or |
| <code>Poll::Pending</code>.</p> |
| <h5 id="examples-5"><a href="#examples-5">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// The buffer is **not** included in the async task and will |
| // only exist on the stack. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| |
| <span class="comment">// Try to recv data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..n]); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_recv_ready" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#735-737">source</a><h4 class="code-header">pub fn <a href="#method.poll_recv_ready" class="fnname">poll_recv_ready</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>>></h4></section></summary><div class="docblock"><p>Polls for read/receive readiness.</p> |
| <p>If the udp stream is not currently ready for receiving, this method will |
| store a clone of the <code>Waker</code> from the provided <code>Context</code>. When the udp |
| socket becomes ready for reading, <code>Waker::wake</code> will be called on the |
| waker.</p> |
| <p>Note that on multiple calls to <code>poll_recv_ready</code>, <code>poll_recv</code> or |
| <code>poll_peek</code>, only the <code>Waker</code> from the <code>Context</code> passed to the most |
| recent call is scheduled to receive a wakeup. (However, |
| <code>poll_send_ready</code> retains a second, independent waker.)</p> |
| <p>This function is intended for cases where creating and pinning a future |
| via <a href="struct.UdpSocket.html#method.readable"><code>readable</code></a> is not feasible. Where possible, using <a href="struct.UdpSocket.html#method.readable"><code>readable</code></a> is |
| preferred, as this supports polling from multiple tasks at once.</p> |
| <h5 id="return-value-2"><a href="#return-value-2">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the udp stream is not ready for reading.</li> |
| <li><code>Poll::Ready(Ok(()))</code> if the udp stream is ready for reading.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-2"><a href="#errors-2">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.recv" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#776-781">source</a><h4 class="code-header">pub async fn <a href="#method.recv" class="fnname">recv</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket from the remote address |
| to which it is connected. On success, returns the number of bytes read.</p> |
| <p>The function must be called with valid byte array <code>buf</code> of sufficient |
| size to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>The <a href="struct.UdpSocket.html#method.connect"><code>connect</code></a> method will connect this socket to a remote address. |
| This method will fail if the socket is not connected.</p> |
| <h5 id="cancel-safety-4"><a href="#cancel-safety-4">Cancel safety</a></h5> |
| <p>This method is cancel safe. If <code>recv</code> is used as the event in a |
| <a href="../macro.select.html"><code>tokio::select!</code></a> statement and some other branch |
| completes first, it is guaranteed that no messages were received on this |
| socket.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Bind socket |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = <span class="macro">vec!</span>[<span class="number">0</span>; <span class="number">10</span>]; |
| <span class="kw">let </span>n = socket.recv(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"received {} bytes {:?}"</span>, n, <span class="kw-2">&</span>buf[..n]); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_recv" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#806-822">source</a><h4 class="code-header">pub fn <a href="#method.poll_recv" class="fnname">poll_recv</a>(<br> &self,<br> cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,<br> buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>>></h4></section></summary><div class="docblock"><p>Attempts to receive a single datagram message on the socket from the remote |
| address to which it is <code>connect</code>ed.</p> |
| <p>The <a href="struct.UdpSocket.html#method.connect"><code>connect</code></a> method will connect this socket to a remote address. This method |
| resolves to an error if the socket is not connected.</p> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the recv direction, only the |
| <code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to |
| receive a wakeup.</p> |
| <h5 id="return-value-3"><a href="#return-value-3">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the socket is not ready to read</li> |
| <li><code>Poll::Ready(Ok(()))</code> reads data <code>ReadBuf</code> if the socket is ready</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-3"><a href="#errors-3">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_recv" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#874-878">source</a><h4 class="code-header">pub fn <a href="#method.try_recv" class="fnname">try_recv</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket from the remote |
| address to which it is connected. On success, returns the number of |
| bytes read.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <h5 id="examples-6"><a href="#examples-6">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// The buffer is **not** included in the async task and will |
| // only exist on the stack. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| |
| <span class="comment">// Try to recv data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_recv(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..n]); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_recv_buf" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#930-946">source</a><h4 class="code-header">pub fn <a href="#method.try_recv_buf" class="fnname">try_recv_buf</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>B) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to receive data from the stream into the provided buffer, advancing the |
| buffer’s internal cursor, returning how many bytes were read.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>This method can be used even if <code>buf</code> is uninitialized.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <h5 id="examples-7"><a href="#examples-7">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">1024</span>); |
| |
| <span class="comment">// Try to recv data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_recv_buf(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>(n) => { |
| <span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..n]); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.recv_buf" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#978-994">source</a><h4 class="code-header">pub async fn <a href="#method.recv_buf" class="fnname">recv_buf</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(&self, buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>B) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket from the remote address |
| to which it is connected, advancing the buffer’s internal cursor, |
| returning how many bytes were read.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>This method can be used even if <code>buf</code> is uninitialized.</p> |
| <h5 id="examples-8"><a href="#examples-8">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">512</span>); |
| <span class="kw">let </span>len = socket.recv_buf(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"received {} bytes {:?}"</span>, len, <span class="kw-2">&</span>buf[..len]); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_recv_buf_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1053-1069">source</a><h4 class="code-header">pub fn <a href="#method.try_recv_buf_from" class="fnname">try_recv_buf_from</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(<br> &self,<br> buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>B<br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket. On success, |
| returns the number of bytes read and the origin.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>This method can be used even if <code>buf</code> is uninitialized.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <h5 id="notes-1"><a href="#notes-1">Notes</a></h5> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| <h5 id="examples-9"><a href="#examples-9">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">1024</span>); |
| |
| <span class="comment">// Try to recv data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_recv_buf_from(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>((n, _addr)) => { |
| <span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..n]); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.recv_buf_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1109-1125">source</a><h4 class="code-header">pub async fn <a href="#method.recv_buf_from" class="fnname">recv_buf_from</a><B: <a class="trait" href="../../bytes/buf/buf_mut/trait.BufMut.html" title="trait bytes::buf::buf_mut::BufMut">BufMut</a>>(<br> &self,<br> buf: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>B<br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket, advancing the |
| buffer’s internal cursor, returning how many bytes were read and the origin.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>This method can be used even if <code>buf</code> is uninitialized.</p> |
| <h5 id="notes-2"><a href="#notes-2">Notes</a></h5> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| <h5 id="examples-10"><a href="#examples-10">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| socket.connect(<span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">512</span>); |
| <span class="kw">let </span>(len, addr) = socket.recv_buf_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"received {:?} bytes from {:?}"</span>, len, addr); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.send_to" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1164-1174">source</a><h4 class="code-header">pub async fn <a href="#method.send_to" class="fnname">send_to</a><A: <a class="trait" href="trait.ToSocketAddrs.html" title="trait tokio::net::ToSocketAddrs">ToSocketAddrs</a>>(<br> &self,<br> buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>],<br> target: A<br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Sends data on the socket to the given address. On success, returns the |
| number of bytes written.</p> |
| <p>Address type can be any implementor of <a href="trait.ToSocketAddrs.html"><code>ToSocketAddrs</code></a> trait. See its |
| documentation for concrete examples.</p> |
| <p>It is possible for <code>addr</code> to yield multiple addresses, but <code>send_to</code> |
| will only send data to the first address yielded by <code>addr</code>.</p> |
| <p>This will return an error when the IP version of the local socket does |
| not match that returned from <a href="trait.ToSocketAddrs.html"><code>ToSocketAddrs</code></a>.</p> |
| <h5 id="cancel-safety-5"><a href="#cancel-safety-5">Cancel safety</a></h5> |
| <p>This method is cancel safe. If <code>send_to</code> is used as the event in a |
| <a href="../macro.select.html"><code>tokio::select!</code></a> statement and some other branch |
| completes first, then it is guaranteed that the message was not sent.</p> |
| <h5 id="example-5"><a href="#example-5">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| <span class="kw">let </span>len = socket.send_to(<span class="string">b"hello world"</span>, <span class="string">"127.0.0.1:8081"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"Sent {} bytes"</span>, len); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_send_to" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1193-1202">source</a><h4 class="code-header">pub fn <a href="#method.poll_send_to" class="fnname">poll_send_to</a>(<br> &self,<br> cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,<br> buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>],<br> target: <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>>></h4></section></summary><div class="docblock"><p>Attempts to send data on the socket to a given address.</p> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the send direction, only the |
| <code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to |
| receive a wakeup.</p> |
| <h5 id="return-value-4"><a href="#return-value-4">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the socket is not ready to write</li> |
| <li><code>Poll::Ready(Ok(n))</code> <code>n</code> is the number of bytes sent.</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-4"><a href="#errors-4">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_send_to" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1251-1255">source</a><h4 class="code-header">pub fn <a href="#method.try_send_to" class="fnname">try_send_to</a>(&self, buf: &[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>], target: <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>></h4></section></summary><div class="docblock"><p>Tries to send data on the socket to the given address, but if the send is |
| blocked this will return right away.</p> |
| <p>This function is usually paired with <code>writable()</code>.</p> |
| <h5 id="returns-1"><a href="#returns-1">Returns</a></h5> |
| <p>If successful, returns the number of bytes sent</p> |
| <p>Users should ensure that when the remote cannot receive, the |
| <a href="https://doc.rust-lang.org/nightly/std/io/error/enum.ErrorKind.html#variant.WouldBlock"><code>ErrorKind::WouldBlock</code></a> is properly handled. An error can also occur |
| if the IP version of the socket does not match that of <code>target</code>.</p> |
| <h5 id="example-6"><a href="#example-6">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::error::Error; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> <span class="prelude-ty">Result</span><(), Box<<span class="kw">dyn </span>Error>> { |
| <span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span>dst = <span class="string">"127.0.0.1:8081"</span>.parse()<span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| socket.writable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">match </span>socket.try_send_to(<span class="kw-2">&</span><span class="string">b"hello world"</span>[..], dst) { |
| <span class="prelude-val">Ok</span>(sent) => { |
| <span class="macro">println!</span>(<span class="string">"sent {} bytes"</span>, sent); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="comment">// Writable false positive. |
| </span><span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => <span class="kw">return </span><span class="prelude-val">Err</span>(e.into()), |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.recv_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1305-1310">source</a><h4 class="code-header">pub async fn <a href="#method.recv_from" class="fnname">recv_from</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Receives a single datagram message on the socket. On success, returns |
| the number of bytes read and the origin.</p> |
| <p>The function must be called with valid byte array <code>buf</code> of sufficient |
| size to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <h5 id="cancel-safety-6"><a href="#cancel-safety-6">Cancel safety</a></h5> |
| <p>This method is cancel safe. If <code>recv_from</code> is used as the event in a |
| <a href="../macro.select.html"><code>tokio::select!</code></a> statement and some other branch |
| completes first, it is guaranteed that no messages were received on this |
| socket.</p> |
| <h5 id="example-7"><a href="#example-7">Example</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">32</span>]; |
| <span class="kw">let </span>(len, addr) = socket.recv_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"received {:?} bytes from {:?}"</span>, len, addr); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| <h5 id="notes-3"><a href="#notes-3">Notes</a></h5> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_recv_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1338-1358">source</a><h4 class="code-header">pub fn <a href="#method.poll_recv_from" class="fnname">poll_recv_from</a>(<br> &self,<br> cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,<br> buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>>></h4></section></summary><div class="docblock"><p>Attempts to receive a single datagram on the socket.</p> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the recv direction, only the |
| <code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to |
| receive a wakeup.</p> |
| <h5 id="return-value-5"><a href="#return-value-5">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the socket is not ready to read</li> |
| <li><code>Poll::Ready(Ok(addr))</code> reads data from <code>addr</code> into <code>ReadBuf</code> if the socket is ready</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-5"><a href="#errors-5">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| <h5 id="notes-4"><a href="#notes-4">Notes</a></h5> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_recv_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1418-1422">source</a><h4 class="code-header">pub fn <a href="#method.try_recv_from" class="fnname">try_recv_from</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Tries to receive a single datagram message on the socket. On success, |
| returns the number of bytes read and the origin.</p> |
| <p>This method must be called with valid byte array buf of sufficient size |
| to hold the message bytes. If a message is too long to fit in the |
| supplied buffer, excess bytes may be discarded.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <h5 id="notes-5"><a href="#notes-5">Notes</a></h5> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| <h5 id="examples-11"><a href="#examples-11">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Connect to a peer |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">loop </span>{ |
| <span class="comment">// Wait for the socket to be readable |
| </span>socket.readable().<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="comment">// The buffer is **not** included in the async task and will |
| // only exist on the stack. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0</span>; <span class="number">1024</span>]; |
| |
| <span class="comment">// Try to recv data, this may still fail with `WouldBlock` |
| // if the readiness event is a false positive. |
| </span><span class="kw">match </span>socket.try_recv_from(<span class="kw-2">&mut </span>buf) { |
| <span class="prelude-val">Ok</span>((n, _addr)) => { |
| <span class="macro">println!</span>(<span class="string">"GOT {:?}"</span>, <span class="kw-2">&</span>buf[..n]); |
| <span class="kw">break</span>; |
| } |
| <span class="prelude-val">Err</span>(<span class="kw-2">ref </span>e) <span class="kw">if </span>e.kind() == io::ErrorKind::WouldBlock => { |
| <span class="kw">continue</span>; |
| } |
| <span class="prelude-val">Err</span>(e) => { |
| <span class="kw">return </span><span class="prelude-val">Err</span>(e); |
| } |
| } |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_io" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1456-1464">source</a><h4 class="code-header">pub fn <a href="#method.try_io" class="fnname">try_io</a><R>(<br> &self,<br> interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>,<br> f: impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R></h4></section></summary><div class="docblock"><p>Tries to read or write from the socket using a user-provided IO operation.</p> |
| <p>If the socket is ready, the provided closure is called. The closure |
| should attempt to perform IO operation on the socket by manually |
| calling the appropriate syscall. If the operation fails because the |
| socket is not actually ready, then the closure should return a |
| <code>WouldBlock</code> error and the readiness flag is cleared. The return value |
| of the closure is then returned by <code>try_io</code>.</p> |
| <p>If the socket is not ready, then the closure is not called |
| and a <code>WouldBlock</code> error is returned.</p> |
| <p>The closure should only return a <code>WouldBlock</code> error if it has performed |
| an IO operation on the socket that failed due to the socket not being |
| ready. Returning a <code>WouldBlock</code> error in any other situation will |
| incorrectly clear the readiness flag, which can cause the socket to |
| behave incorrectly.</p> |
| <p>The closure should not perform the IO operation using any of the methods |
| defined on the Tokio <code>UdpSocket</code> type, as this will mess with the |
| readiness flag and can cause the socket to behave incorrectly.</p> |
| <p>This method is not intended to be used with combined interests. |
| The closure should perform only one type of IO operation, so it should not |
| require more than one ready state. This method may panic or sleep forever |
| if it is called with a combined interest.</p> |
| <p>Usually, <a href="struct.UdpSocket.html#method.readable"><code>readable()</code></a>, <a href="struct.UdpSocket.html#method.writable"><code>writable()</code></a> or <a href="struct.UdpSocket.html#method.ready"><code>ready()</code></a> is used with this function.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.async_io" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1491-1500">source</a><h4 class="code-header">pub async fn <a href="#method.async_io" class="fnname">async_io</a><R>(<br> &self,<br> interest: <a class="struct" href="../io/struct.Interest.html" title="struct tokio::io::Interest">Interest</a>,<br> f: impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>() -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><R></h4></section></summary><div class="docblock"><p>Reads or writes from the socket using a user-provided IO operation.</p> |
| <p>The readiness of the socket is awaited and when the socket is ready, |
| the provided closure is called. The closure should attempt to perform |
| IO operation on the socket by manually calling the appropriate syscall. |
| If the operation fails because the socket is not actually ready, |
| then the closure should return a <code>WouldBlock</code> error. In such case the |
| readiness flag is cleared and the socket readiness is awaited again. |
| This loop is repeated until the closure returns an <code>Ok</code> or an error |
| other than <code>WouldBlock</code>.</p> |
| <p>The closure should only return a <code>WouldBlock</code> error if it has performed |
| an IO operation on the socket that failed due to the socket not being |
| ready. Returning a <code>WouldBlock</code> error in any other situation will |
| incorrectly clear the readiness flag, which can cause the socket to |
| behave incorrectly.</p> |
| <p>The closure should not perform the IO operation using any of the methods |
| defined on the Tokio <code>UdpSocket</code> type, as this will mess with the |
| readiness flag and can cause the socket to behave incorrectly.</p> |
| <p>This method is not intended to be used with combined interests. |
| The closure should perform only one type of IO operation, so it should not |
| require more than one ready state. This method may panic or sleep forever |
| if it is called with a combined interest.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peek_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1546-1551">source</a><h4 class="code-header">pub async fn <a href="#method.peek_from" class="fnname">peek_from</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Receives data from the socket, without removing it from the input queue. |
| On success, returns the number of bytes read and the address from whence |
| the data came.</p> |
| <h5 id="notes-6"><a href="#notes-6">Notes</a></h5> |
| <p>On Windows, if the data is larger than the buffer specified, the buffer |
| is filled with the first part of the data, and peek_from returns the error |
| WSAEMSGSIZE(10040). The excess data is lost. |
| Make sure to always use a sufficiently large buffer to hold the |
| maximum UDP packet size, which can be up to 65536 bytes in size.</p> |
| <p>MacOS will return an error if you pass a zero-sized buffer.</p> |
| <p>If you’re merely interested in learning the sender of the data at the head of the queue, |
| try <a href="struct.UdpSocket.html#method.peek_sender"><code>peek_sender</code></a>.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| <h5 id="examples-12"><a href="#examples-12">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>buf = <span class="macro">vec!</span>[<span class="number">0u8</span>; <span class="number">32</span>]; |
| <span class="kw">let </span>(len, addr) = socket.peek_from(<span class="kw-2">&mut </span>buf).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"peeked {:?} bytes from {:?}"</span>, len, addr); |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_peek_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1593-1613">source</a><h4 class="code-header">pub fn <a href="#method.poll_peek_from" class="fnname">poll_peek_from</a>(<br> &self,<br> cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,<br> buf: &mut <a class="struct" href="../io/struct.ReadBuf.html" title="struct tokio::io::ReadBuf">ReadBuf</a><'_><br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>>></h4></section></summary><div class="docblock"><p>Receives data from the socket, without removing it from the input queue. |
| On success, returns the sending address of the datagram.</p> |
| <h5 id="notes-7"><a href="#notes-7">Notes</a></h5> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the recv direction, only the |
| <code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to |
| receive a wakeup</p> |
| <p>On Windows, if the data is larger than the buffer specified, the buffer |
| is filled with the first part of the data, and peek returns the error |
| WSAEMSGSIZE(10040). The excess data is lost. |
| Make sure to always use a sufficiently large buffer to hold the |
| maximum UDP packet size, which can be up to 65536 bytes in size.</p> |
| <p>MacOS will return an error if you pass a zero-sized buffer.</p> |
| <p>If you’re merely interested in learning the sender of the data at the head of the queue, |
| try <a href="struct.UdpSocket.html#method.poll_peek_sender"><code>poll_peek_sender</code></a>.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| <h5 id="return-value-6"><a href="#return-value-6">Return value</a></h5> |
| <p>The function returns:</p> |
| <ul> |
| <li><code>Poll::Pending</code> if the socket is not ready to read</li> |
| <li><code>Poll::Ready(Ok(addr))</code> reads data from <code>addr</code> into <code>ReadBuf</code> if the socket is ready</li> |
| <li><code>Poll::Ready(Err(e))</code> if an error is encountered.</li> |
| </ul> |
| <h5 id="errors-6"><a href="#errors-6">Errors</a></h5> |
| <p>This function may encounter any standard I/O error except <code>WouldBlock</code>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_peek_from" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1643-1647">source</a><h4 class="code-header">pub fn <a href="#method.try_peek_from" class="fnname">try_peek_from</a>(&self, buf: &mut [<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><(<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, <a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>)></h4></section></summary><div class="docblock"><p>Tries to receive data on the socket without removing it from the input queue. |
| On success, returns the number of bytes read and the sending address of the |
| datagram.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <h5 id="notes-8"><a href="#notes-8">Notes</a></h5> |
| <p>On Windows, if the data is larger than the buffer specified, the buffer |
| is filled with the first part of the data, and peek returns the error |
| WSAEMSGSIZE(10040). The excess data is lost. |
| Make sure to always use a sufficiently large buffer to hold the |
| maximum UDP packet size, which can be up to 65536 bytes in size.</p> |
| <p>MacOS will return an error if you pass a zero-sized buffer.</p> |
| <p>If you’re merely interested in learning the sender of the data at the head of the queue, |
| try <a href="struct.UdpSocket.html#method.try_peek_sender"><code>try_peek_sender</code></a>.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peek_sender" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1662-1667">source</a><h4 class="code-header">pub async fn <a href="#method.peek_sender" class="fnname">peek_sender</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Retrieve the sender of the data at the head of the input queue, waiting if empty.</p> |
| <p>This is equivalent to calling <a href="struct.UdpSocket.html#method.peek_from"><code>peek_from</code></a> with a zero-sized buffer, |
| but suppresses the <code>WSAEMSGSIZE</code> error on Windows and the “invalid argument” error on macOS.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_peek_sender" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1689-1693">source</a><h4 class="code-header">pub fn <a href="#method.poll_peek_sender" class="fnname">poll_peek_sender</a>(&self, cx: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>>></h4></section></summary><div class="docblock"><p>Retrieve the sender of the data at the head of the input queue, |
| scheduling a wakeup if empty.</p> |
| <p>This is equivalent to calling <a href="struct.UdpSocket.html#method.poll_peek_from"><code>poll_peek_from</code></a> with a zero-sized buffer, |
| but suppresses the <code>WSAEMSGSIZE</code> error on Windows and the “invalid argument” error on macOS.</p> |
| <h5 id="notes-9"><a href="#notes-9">Notes</a></h5> |
| <p>Note that on multiple calls to a <code>poll_*</code> method in the recv direction, only the |
| <code>Waker</code> from the <code>Context</code> passed to the most recent call will be scheduled to |
| receive a wakeup.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_peek_sender" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1707-1711">source</a><h4 class="code-header">pub fn <a href="#method.try_peek_sender" class="fnname">try_peek_sender</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/std/net/socket_addr/enum.SocketAddr.html" title="enum std::net::socket_addr::SocketAddr">SocketAddr</a>></h4></section></summary><div class="docblock"><p>Try to retrieve the sender of the data at the head of the input queue.</p> |
| <p>When there is no pending data, <code>Err(io::ErrorKind::WouldBlock)</code> is |
| returned. This function is usually paired with <code>readable()</code>.</p> |
| <p>Note that the socket address <strong>cannot</strong> be implicitly trusted, because it is relatively |
| trivial to send a UDP datagram with a spoofed origin in a <a href="https://en.wikipedia.org/wiki/Packet_injection">packet injection attack</a>. |
| Because UDP is stateless and does not validate the origin of a packet, |
| the attacker does not need to be able to intercept traffic in order to interfere. |
| It is important to be aware of this when designing your application-level protocol.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.broadcast" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1731-1733">source</a><h4 class="code-header">pub fn <a href="#method.broadcast" class="fnname">broadcast</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>SO_BROADCAST</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_broadcast"><code>set_broadcast</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_broadcast" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1739-1741">source</a><h4 class="code-header">pub fn <a href="#method.set_broadcast" class="fnname">set_broadcast</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>SO_BROADCAST</code> option for this socket.</p> |
| <p>When enabled, this socket is allowed to send packets to a broadcast |
| address.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.multicast_loop_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1748-1750">source</a><h4 class="code-header">pub fn <a href="#method.multicast_loop_v4" class="fnname">multicast_loop_v4</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_MULTICAST_LOOP</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_multicast_loop_v4"><code>set_multicast_loop_v4</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_multicast_loop_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1759-1761">source</a><h4 class="code-header">pub fn <a href="#method.set_multicast_loop_v4" class="fnname">set_multicast_loop_v4</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IP_MULTICAST_LOOP</code> option for this socket.</p> |
| <p>If enabled, multicast packets will be looped back to the local socket.</p> |
| <h5 id="note"><a href="#note">Note</a></h5> |
| <p>This may not have any affect on IPv6 sockets.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.multicast_ttl_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1768-1770">source</a><h4 class="code-header">pub fn <a href="#method.multicast_ttl_v4" class="fnname">multicast_ttl_v4</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_MULTICAST_TTL</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_multicast_ttl_v4"><code>set_multicast_ttl_v4</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_multicast_ttl_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1781-1783">source</a><h4 class="code-header">pub fn <a href="#method.set_multicast_ttl_v4" class="fnname">set_multicast_ttl_v4</a>(&self, ttl: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IP_MULTICAST_TTL</code> option for this socket.</p> |
| <p>Indicates the time-to-live value of outgoing multicast packets for |
| this socket. The default value is 1 which means that multicast packets |
| don’t leave the local network unless explicitly requested.</p> |
| <h5 id="note-1"><a href="#note-1">Note</a></h5> |
| <p>This may not have any affect on IPv6 sockets.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.multicast_loop_v6" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1790-1792">source</a><h4 class="code-header">pub fn <a href="#method.multicast_loop_v6" class="fnname">multicast_loop_v6</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IPV6_MULTICAST_LOOP</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_multicast_loop_v6"><code>set_multicast_loop_v6</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_multicast_loop_v6" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1801-1803">source</a><h4 class="code-header">pub fn <a href="#method.set_multicast_loop_v6" class="fnname">set_multicast_loop_v6</a>(&self, on: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value of the <code>IPV6_MULTICAST_LOOP</code> option for this socket.</p> |
| <p>Controls whether this socket sees the multicast packets it sends itself.</p> |
| <h5 id="note-2"><a href="#note-2">Note</a></h5> |
| <p>This may not have any affect on IPv4 sockets.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ttl" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1824-1826">source</a><h4 class="code-header">pub fn <a href="#method.ttl" class="fnname">ttl</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_TTL</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_ttl"><code>set_ttl</code></a>.</p> |
| <h5 id="examples-13"><a href="#examples-13">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="macro">println!</span>(<span class="string">"{:?}"</span>, sock.ttl()<span class="question-mark">?</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_ttl" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1846-1848">source</a><h4 class="code-header">pub fn <a href="#method.set_ttl" class="fnname">set_ttl</a>(&self, ttl: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>IP_TTL</code> option on this socket.</p> |
| <p>This value sets the time-to-live field that is used in every packet sent |
| from this socket.</p> |
| <h5 id="examples-14"><a href="#examples-14">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| |
| <span class="kw">let </span>sock = UdpSocket::bind(<span class="string">"127.0.0.1:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| sock.set_ttl(<span class="number">60</span>)<span class="question-mark">?</span>; |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.tos" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1874-1876">source</a><h4 class="code-header">pub fn <a href="#method.tos" class="fnname">tos</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>></h4></section></summary><div class="docblock"><p>Gets the value of the <code>IP_TOS</code> option for this socket.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.set_tos"><code>set_tos</code></a>.</p> |
| <p><strong>NOTE:</strong> On Windows, <code>IP_TOS</code> is only supported on <a href="https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options">Windows 8+ or |
| Windows Server 2012+.</a></p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.set_tos" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1901-1903">source</a><h4 class="code-header">pub fn <a href="#method.set_tos" class="fnname">set_tos</a>(&self, tos: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>IP_TOS</code> option on this socket.</p> |
| <p>This value sets the type-of-service field that is used in every packet |
| sent from this socket.</p> |
| <p><strong>NOTE:</strong> On Windows, <code>IP_TOS</code> is only supported on <a href="https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options">Windows 8+ or |
| Windows Server 2012+.</a></p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.device" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1913-1915">source</a><h4 class="code-header">pub fn <a href="#method.device" class="fnname">device</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>>>></h4></section></summary><div class="docblock"><p>Gets the value for the <code>SO_BINDTODEVICE</code> option on this socket</p> |
| <p>This value gets the socket-bound device’s interface name.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.bind_device" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1929-1931">source</a><h4 class="code-header">pub fn <a href="#method.bind_device" class="fnname">bind_device</a>(&self, interface: <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&[<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>]>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Sets the value for the <code>SO_BINDTODEVICE</code> option on this socket</p> |
| <p>If a socket is bound to an interface, only packets received from that |
| particular interface are processed by the socket. Note that this only |
| works for some socket types, particularly <code>AF_INET</code> sockets.</p> |
| <p>If <code>interface</code> is <code>None</code> or an empty string it removes the binding.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.join_multicast_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1940-1942">source</a><h4 class="code-header">pub fn <a href="#method.join_multicast_v4" class="fnname">join_multicast_v4</a>(<br> &self,<br> multiaddr: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv4Addr.html" title="struct std::net::ip_addr::Ipv4Addr">Ipv4Addr</a>,<br> interface: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv4Addr.html" title="struct std::net::ip_addr::Ipv4Addr">Ipv4Addr</a><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IP_ADD_MEMBERSHIP</code> type.</p> |
| <p>This function specifies a new multicast group for this socket to join. |
| The address must be a valid multicast address, and <code>interface</code> is the |
| address of the local interface with which the system should join the |
| multicast group. If it’s equal to <code>INADDR_ANY</code> then an appropriate |
| interface is chosen by the system.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.join_multicast_v6" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1949-1951">source</a><h4 class="code-header">pub fn <a href="#method.join_multicast_v6" class="fnname">join_multicast_v6</a>(<br> &self,<br> multiaddr: &<a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv6Addr.html" title="struct std::net::ip_addr::Ipv6Addr">Ipv6Addr</a>,<br> interface: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IPV6_ADD_MEMBERSHIP</code> type.</p> |
| <p>This function specifies a new multicast group for this socket to join. |
| The address must be a valid multicast address, and <code>interface</code> is the |
| index of the interface to join/leave (or 0 to indicate any interface).</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.leave_multicast_v4" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1958-1960">source</a><h4 class="code-header">pub fn <a href="#method.leave_multicast_v4" class="fnname">leave_multicast_v4</a>(<br> &self,<br> multiaddr: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv4Addr.html" title="struct std::net::ip_addr::Ipv4Addr">Ipv4Addr</a>,<br> interface: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv4Addr.html" title="struct std::net::ip_addr::Ipv4Addr">Ipv4Addr</a><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IP_DROP_MEMBERSHIP</code> type.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.join_multicast_v4"><code>join_multicast_v4</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.leave_multicast_v6" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1967-1969">source</a><h4 class="code-header">pub fn <a href="#method.leave_multicast_v6" class="fnname">leave_multicast_v6</a>(<br> &self,<br> multiaddr: &<a class="struct" href="https://doc.rust-lang.org/nightly/std/net/ip_addr/struct.Ipv6Addr.html" title="struct std::net::ip_addr::Ipv6Addr">Ipv6Addr</a>,<br> interface: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a><br>) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Executes an operation of the <code>IPV6_DROP_MEMBERSHIP</code> type.</p> |
| <p>For more information about this option, see <a href="struct.UdpSocket.html#method.join_multicast_v6"><code>join_multicast_v6</code></a>.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.take_error" class="method has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1990-1992">source</a><h4 class="code-header">pub fn <a href="#method.take_error" class="fnname">take_error</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/io/error/type.Result.html" title="type std::io::error::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a>>></h4></section></summary><div class="docblock"><p>Returns the value of the <code>SO_ERROR</code> option.</p> |
| <h5 id="examples-15"><a href="#examples-15">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::net::UdpSocket; |
| <span class="kw">use </span>std::io; |
| |
| <span class="attribute">#[tokio::main] |
| </span><span class="kw">async fn </span>main() -> io::Result<()> { |
| <span class="comment">// Create a socket |
| </span><span class="kw">let </span>socket = UdpSocket::bind(<span class="string">"0.0.0.0:8080"</span>).<span class="kw">await</span><span class="question-mark">?</span>; |
| |
| <span class="kw">if let </span><span class="prelude-val">Ok</span>(<span class="prelude-val">Some</span>(err)) = socket.take_error() { |
| <span class="macro">println!</span>(<span class="string">"Got error: {:?}"</span>, err); |
| } |
| |
| <span class="prelude-val">Ok</span>(()) |
| }</code></pre></div> |
| </div></details></div></details></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor"></a></h2><div id="trait-implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsFd-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2025-2029">source</a><a href="#impl-AsFd-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/std/os/fd/owned/trait.AsFd.html" title="trait std::os::fd::owned::AsFd">AsFd</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_fd" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2026-2028">source</a><a href="#method.as_fd" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd" class="fnname">as_fd</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/std/os/fd/owned/struct.BorrowedFd.html" title="struct std::os::fd::owned::BorrowedFd">BorrowedFd</a><'_></h4></section></summary><div class='docblock'>Borrows the file descriptor. <a href="https://doc.rust-lang.org/nightly/std/os/fd/owned/trait.AsFd.html#tymethod.as_fd">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AsRawFd-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2018-2022">source</a><a href="#impl-AsRawFd-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/std/os/fd/raw/trait.AsRawFd.html" title="trait std::os::fd::raw::AsRawFd">AsRawFd</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.as_raw_fd" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2019-2021">source</a><a href="#method.as_raw_fd" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd" class="fnname">as_raw_fd</a>(&self) -> <a class="type" href="https://doc.rust-lang.org/nightly/std/os/fd/raw/type.RawFd.html" title="type std::os::fd::raw::RawFd">RawFd</a></h4></section></summary><div class='docblock'>Extracts the raw file descriptor. <a href="https://doc.rust-lang.org/nightly/std/os/fd/raw/trait.AsRawFd.html#tymethod.as_raw_fd">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Debug-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2007-2011">source</a><a href="#impl-Debug-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2008-2010">source</a><a href="#method.fmt" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt" class="fnname">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/nightly/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-TryFrom%3CUdpSocket%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#1995-2005">source</a><a href="#impl-TryFrom%3CUdpSocket%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><<a class="struct" href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html" title="struct std::net::udp::UdpSocket">UdpSocket</a>> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tokio/net/udp.rs.html#2002-2004">source</a><a href="#method.try_from" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#tymethod.try_from" class="fnname">try_from</a>(stream: <a class="struct" href="https://doc.rust-lang.org/nightly/std/net/udp/struct.UdpSocket.html" title="struct std::net::udp::UdpSocket">UdpSocket</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class="docblock"><p>Consumes stream, returning the tokio I/O object.</p> |
| <p>This is equivalent to |
| <a href="struct.UdpSocket.html#method.from_std"><code>UdpSocket::from_std(stream)</code></a>.</p> |
| </div></details><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error" class="anchor"></a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="struct" href="https://doc.rust-lang.org/nightly/std/io/error/struct.Error.html" title="struct std::io::error::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details></div></details></div><h2 id="synthetic-implementations" class="small-section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor"></a></h2><div id="synthetic-implementations-list"><section id="impl-RefUnwindSafe-for-UdpSocket" class="impl has-srclink"><a href="#impl-RefUnwindSafe-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section><section id="impl-Send-for-UdpSocket" class="impl has-srclink"><a href="#impl-Send-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section><section id="impl-Sync-for-UdpSocket" class="impl has-srclink"><a href="#impl-Sync-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section><section id="impl-Unpin-for-UdpSocket" class="impl has-srclink"><a href="#impl-Unpin-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section><section id="impl-UnwindSafe-for-UdpSocket" class="impl has-srclink"><a href="#impl-UnwindSafe-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl <a class="trait" href="https://doc.rust-lang.org/nightly/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.UdpSocket.html" title="struct tokio::net::UdpSocket">UdpSocket</a></h3></section></div><h2 id="blanket-implementations" class="small-section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor"></a></h2><div id="blanket-implementations-list"><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Any-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/any.rs.html#200">source</a><a href="#impl-Any-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<span class="where fmt-newline">where<br> T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/any.rs.html#201">source</a><a href="#method.type_id" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id" class="fnname">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/nightly/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#211">source</a><a href="#impl-Borrow%3CT%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a><T> for T<span class="where fmt-newline">where<br> T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#213">source</a></span><a href="#method.borrow" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow" class="fnname">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&</a>T</h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#220">source</a><a href="#impl-BorrowMut%3CT%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a><T> for T<span class="where fmt-newline">where<br> T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#221">source</a></span><a href="#method.borrow_mut" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fnname">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>T</h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#733">source</a><a href="#impl-From%3CT%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for T</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#736">source</a></span><a href="#method.from" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html#tymethod.from" class="fnname">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#717">source</a><a href="#impl-Into%3CU%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><U> for T<span class="where fmt-newline">where<br> U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a><T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#725">source</a></span><a href="#method.into" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html#tymethod.into" class="fnname">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p> |
| <p>That is, this conversion is whatever the implementation of |
| <code><a href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="From">From</a><T> for U</code> chooses to do.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#775">source</a><a href="#impl-TryFrom%3CU%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U> for T<span class="where fmt-newline">where<br> U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-2" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-2" class="anchor"></a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from-1" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#781">source</a></span><a href="#method.try_from-1" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#tymethod.try_from" class="fnname">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-UdpSocket" class="impl has-srclink"><a class="srclink rightside" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#760">source</a><a href="#impl-TryInto%3CU%3E-for-UdpSocket" class="anchor"></a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a><U> for T<span class="where fmt-newline">where<br> U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-1" class="anchor"></a><h4 class="code-header">type <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl has-srclink"><span class="rightside"><span class="since" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span> · <a class="srclink" href="https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#766">source</a></span><a href="#method.try_into" class="anchor"></a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#tymethod.try_into" class="fnname">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tokio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |