/*! The networking methods for our client components to call. In particular, this file provides a send() method to handle connecting to a server process and sending it data. */ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; use std::str; // may need to change strings to byte vectors in the future pub async fn send(addr: String, str: String) -> String { let mut stream = TcpStream::connect(&addr) .await .expect("Failed to create TcpStream"); // send data stream .write_all(str.as_bytes()) .await .expect("Failed to write data to stream"); // read response let mut buf = vec![0; 1024]; let n = stream .read(&mut buf) .await .expect("Failed to read data from socket"); if n == 0 { return "".to_string(); } str::from_utf8(&buf[0..n]) .expect("Invalid UTF-8 sequence") .to_string() }