Directly pass socket address strings to the connect/bind functions

pull/5/head
Sebastian Dröge 5 years ago
parent c2374308b0
commit 5613f9e47d
  1. 9
      examples/autobahn-server.rs
  2. 8
      examples/echo-server.rs
  3. 34
      tests/communication.rs
  4. 18
      tests/handshakes.rs

@ -1,4 +1,4 @@
use async_std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
use async_std::net::{SocketAddr, TcpListener, TcpStream};
use async_tungstenite::accept_async;
use futures::{SinkExt, StreamExt};
use log::*;
@ -19,12 +19,7 @@ async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
async fn run() {
env_logger::init();
let addr = "127.0.0.1:9002"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
let addr = "127.0.0.1:9002";
let listener = TcpListener::bind(&addr).await.unwrap();
info!("Listening on: {}", addr);

@ -10,7 +10,7 @@
use std::{env, io::Error};
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs};
use async_std::net::{TcpListener, TcpStream};
use async_std::task;
use futures::StreamExt;
use log::info;
@ -20,12 +20,6 @@ async fn run() -> Result<(), Error> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
let addr = addr
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;

@ -1,4 +1,4 @@
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs};
use async_std::net::{TcpListener, TcpStream};
use async_std::task;
use async_tungstenite::{accept_async, client_async, WebSocketStream};
use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
@ -30,13 +30,7 @@ async fn communication() {
let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
let f = async move {
let address = "0.0.0.0:12345"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
let listener = TcpListener::bind("0.0.0.0:12345").await.unwrap();
info!("Server ready");
con_tx.send(()).unwrap();
info!("Waiting on next connection");
@ -51,13 +45,7 @@ async fn communication() {
info!("Waiting for server to be ready");
con_rx.await.expect("Server not ready");
let address = "0.0.0.0:12345"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
let tcp = TcpStream::connect("0.0.0.0:12345")
.await
.expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap();
@ -88,13 +76,7 @@ async fn split_communication() {
let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
let f = async move {
let address = "0.0.0.0:12346"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
let listener = TcpListener::bind("0.0.0.0:12346").await.unwrap();
info!("Server ready");
con_tx.send(()).unwrap();
info!("Waiting on next connection");
@ -109,13 +91,7 @@ async fn split_communication() {
info!("Waiting for server to be ready");
con_rx.await.expect("Server not ready");
let address = "0.0.0.0:12346"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
let tcp = TcpStream::connect("0.0.0.0:12346")
.await
.expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap();

@ -1,4 +1,4 @@
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs};
use async_std::net::{TcpListener, TcpStream};
use async_std::task;
use async_tungstenite::{accept_async, client_async};
@ -7,13 +7,7 @@ async fn handshakes() {
let (tx, rx) = futures::channel::oneshot::channel();
let f = async move {
let address = "0.0.0.0:12345"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
let listener = TcpListener::bind("0.0.0.0:12345").await.unwrap();
tx.send(()).unwrap();
while let Ok((connection, _)) = listener.accept().await {
let stream = accept_async(connection).await;
@ -24,13 +18,7 @@ async fn handshakes() {
task::spawn(f);
rx.await.expect("Failed to wait for server to be ready");
let address = "0.0.0.0:12345"
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
let tcp = TcpStream::connect("0.0.0.0:12345")
.await
.expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap();

Loading…
Cancel
Save