#TCP connection established, but can't communicate?

12 messages · Page 1 of 1 (latest)

remote dagger
#

I've got code running in two containers and I am able to establish a TCP connection between them over the docker network, but nothing that is sent afterwards seems to be received. I have no idea why. Any tips or advice is welcome.

#

Here's the code which is supposed to receive as of now. It seems to be stuck within the lines.next().await call.

use std::io;
use async_std::net::*;
use async_std::io::BufReader;
use futures::io::{WriteHalf, ReadHalf};
use futures::{StreamExt, AsyncReadExt, join, AsyncBufReadExt};

#[async_std::main]
async fn main() -> io::Result<()> {
    // 
    let stream = TcpStream::connect("container-2:8080").await?;
    let (readhalf, writehalf) = stream.split();
    println!("Connected");

    join!(handle_read(readhalf), handle_write(writehalf));

    Ok(())
}

async fn handle_read(readhalf : ReadHalf<TcpStream>) {
    let reader = BufReader::new(readhalf);
    let mut lines_iter = reader.lines();

    while let Some(line_option) = lines_iter.next().await {
        if let Ok(line) = line_option {
            println!("Received: {}", line);
        } else {
            println!("Failed to read")
        }
    }
}

async fn handle_write(stream : WriteHalf<TcpStream>) {
    todo!()
}
#

Here's the sender:

use std::io::{self, stdin};
use async_std::net::*;
use async_std::io::{BufReader, WriteExt};
use futures::io::{WriteHalf, ReadHalf};
use futures::{StreamExt, AsyncReadExt, join, AsyncBufReadExt};

#[async_std::main]
async fn main() -> io::Result<()> {
    let listener = TcpListener::bind("0.0.0.0:8080").await?;
    println!("Listening");

    listener
        .incoming()
        .for_each_concurrent(None, |stream : Result<TcpStream, _>| async move {
              let (readhalf, writehalf) = stream.unwrap().split();
              join!(handle_write(writehalf), handle_read(readhalf));
        }).await;

    Ok(())
}

async fn handle_read(reader : ReadHalf<TcpStream>) {
    let reader = BufReader::new(reader);
    let mut lines_iter = reader.lines();
    while let Some(line_option) = lines_iter.next().await {
        todo!();
    }
}

async fn handle_write(mut writer : WriteHalf<TcpStream>) {
    println!("Connected")
    let stdin = stdin();
    let mut buf = String::new();
    loop {
        buf.clear();
        stdin.read_line(&mut buf).unwrap();

        writer.write_all(buf.as_bytes());
    }
}
analog solstice
#

(in the sender)

#

You should be getting a warning about something like "Futures do nothing unless polled" on that line

remote dagger
#

No warning though, weirdly

analog solstice
#

you should note that async_std, is basically abandonware afaik