want to bind the Standard IO stream to TCP stream Working ex:
fn main() {
let mut stream =TcpStream::connect("localhost:1912").unwrap();
let stdin = io::stdin();
for command in stdin.lock().lines() {
{
let mut cmd = command.unwrap();
cmd = cmd.add("\n");
_ = stream.write(cmd.as_bytes()).unwrap();
}
{
let mut resp_data = [0 as u8; 250];
_ = stream.read(&mut resp_data).unwrap();
let text = from_utf8(&resp_data).unwrap();
print!("{}", text);
}
}
}
Same I want to do in multi-thread like : not working:
fn main() {
let stream = Arc::new(Mutex::new(TcpStream::connect("localhost:1912").unwrap()));
let str_clone = stream.clone();
thread::spawn(move || {
let mut stout = io::stdout();
let mut str = str_clone.lock().unwrap();
_ = io::copy(&mut *str, &mut stout);
});
let mut stdin = io::stdin();
let mut str = stream.lock().unwrap();
_ = io::copy(&mut stdin, &mut *str);
}
I know that one thread is holding a lock so I am not able to access this on another thread. I am new to Rust so need to know that best way to do this
Ex: This same I was able to do in GoLang like this
func main() {
c, _ := net.Dial("tcp", ":1912")
go io.Copy(os.Stdout, c)
io.Copy(c, os.Stdin)
}