#TCP connection established, but can't communicate?
12 messages · Page 1 of 1 (latest)
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());
}
}
You are not awaiting the result of the write_all call, which means it's never executed
(in the sender)
You should be getting a warning about something like "Futures do nothing unless polled" on that line
Ahhh, thank you. So stupid 😅 .
No warning though, weirdly
ah you're using async_std, apparently they never bothered to annotate their Future types to make this sort of warning work
you should note that async_std, is basically abandonware afaik
Ah, ooops
