summaryrefslogtreecommitdiffstats
path: root/examples/udp-client.rs
diff options
context:
space:
mode:
authorEliza Weisman <eliza@buoyant.io>2019-03-22 15:25:42 -0700
committerGitHub <noreply@github.com>2019-03-22 15:25:42 -0700
commit30330da11a56dfdd11bdbef50dba073a9edc36b2 (patch)
treebf4e8e90293a3c75a2bf5281572e1c01eceab3cb /examples/udp-client.rs
parent6e4945025cdc6f2b71d9b30aaa23c5517cca1504 (diff)
chore: Fix examples not working with `cargo run` (#998)
* chore: Fix examples not working with `cargo run` ## Motivation PR #991 moved the `tokio` crate to its own subdirectory, but did not move the `examples` directory into `tokio/examples`. While attempting to use the examples for testing another change, I noticed that #991 had broken the ability to use `cargo run`, as the examples were no longer considered part of a crate that cargo was aware of: ``` tokio on master [$] via 🦀v1.33.0 at ☸️ aks-eliza-dev ➜ cargo run --example chat error: no example target named `chat` Did you mean `echo`? ``` ## Solution This branch moves the examples into the `tokio` directory, so cargo is now once again aware of them: ``` tokio on eliza/fix-examples [$] via 🦀v1.33.0 at ☸️ aks-eliza-dev ➜ cargo run --example chat Compiling tokio-executor v0.1.7 (/Users/eliza/Code/tokio/tokio-executor) Compiling tokio-reactor v0.1.9 Compiling tokio-threadpool v0.1.13 Compiling tokio-current-thread v0.1.6 Compiling tokio-timer v0.2.10 Compiling tokio-uds v0.2.5 Compiling tokio-udp v0.1.3 Compiling tokio-tcp v0.1.3 Compiling tokio-fs v0.1.6 Compiling tokio v0.1.18 (/Users/eliza/Code/tokio/tokio) Finished dev [unoptimized + debuginfo] target(s) in 7.04s Running `target/debug/examples/chat` server running on localhost:6142 ``` Signed-off-by: Eliza Weisman <eliza@buoyant.io> Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Diffstat (limited to 'examples/udp-client.rs')
-rw-r--r--examples/udp-client.rs70
1 files changed, 0 insertions, 70 deletions
diff --git a/examples/udp-client.rs b/examples/udp-client.rs
deleted file mode 100644
index 900d3616..00000000
--- a/examples/udp-client.rs
+++ /dev/null
@@ -1,70 +0,0 @@
-//! A UDP client that just sends everything it gets via `stdio` in a single datagram, and then
-//! waits for a reply.
-//!
-//! For the reasons of simplicity data from `stdio` is read until `EOF` in a blocking manner.
-//!
-//! You can test this out by running an echo server:
-//!
-//! ```
-//! $ cargo run --example echo-udp -- 127.0.0.1:8080
-//! ```
-//!
-//! and running the client in another terminal:
-//!
-//! ```
-//! $ cargo run --example udp-client
-//! ```
-//!
-//! You can optionally provide any custom endpoint address for the client:
-//!
-//! ```
-//! $ cargo run --example udp-client -- 127.0.0.1:8080
-//! ```
-//!
-//! Don't forget to pass `EOF` to the standard input of the client!
-//!
-//! Please mind that since the UDP protocol doesn't have any capabilities to detect a broken
-//! connection the server needs to be run first, otherwise the client will block forever.
-
-extern crate futures;
-extern crate tokio;
-
-use std::env;
-use std::io::stdin;
-use std::net::SocketAddr;
-use tokio::net::UdpSocket;
-use tokio::prelude::*;
-
-fn get_stdin_data() -> Result<Vec<u8>, Box<std::error::Error>> {
- let mut buf = Vec::new();
- stdin().read_to_end(&mut buf)?;
- Ok(buf)
-}
-
-fn main() -> Result<(), Box<std::error::Error>> {
- let remote_addr: SocketAddr = env::args()
- .nth(1)
- .unwrap_or("127.0.0.1:8080".into())
- .parse()?;
- // We use port 0 to let the operating system allocate an available port for us.
- let local_addr: SocketAddr = if remote_addr.is_ipv4() {
- "0.0.0.0:0"
- } else {
- "[::]:0"
- }
- .parse()?;
- let socket = UdpSocket::bind(&local_addr)?;
- const MAX_DATAGRAM_SIZE: usize = 65_507;
- socket
- .send_dgram(get_stdin_data()?, &remote_addr)
- .and_then(|(socket, _)| socket.recv_dgram(vec![0u8; MAX_DATAGRAM_SIZE]))
- .map(|(_, data, len, _)| {
- println!(
- "Received {} bytes:\n{}",
- len,
- String::from_utf8_lossy(&data[..len])
- )
- })
- .wait()?;
- Ok(())
-}