2

My TCPStream code is like this:

use std::error::Error;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;

#[tokio::main]
pub async fn match_tcp_client(address: String, self_ip: String) -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    println!("client to connect at {}", address);
    let mut stream = TcpStream::connect(address.clone()).await?;
    println!("client done");
    // Write some data.
    stream.write_all(self_ip.as_bytes()).await?;
    stream.write_all(b"hello world!EOF").await?;
    // stream.shutdown().await?;
    Ok(())
}

My question is, is there a way for client to know if server has correctly received the data, and if not resend the data?

xamgore
  • 1,723
  • 1
  • 15
  • 30
Zubayr
  • 457
  • 4
  • 18
  • I *think*, but I'm not quite sure, that this is handled by the TCP protocol, that is, using TCP already means that the other party is supposed to acknowledge that they received the data and, in case they don't, it will send again the data. – jthulhu May 06 '23 at 05:44

1 Answers1

1

TCP provides reliable data transfer. Using flow control, sequence numbers, acknowledgments, and timers, TCP ensures that data is delivered, from sending process to receiving process, correctly and in order.

If you want a secure solution preventing man-in-the-middle attacks, you may use TLS protocol in tokio. Also, you can send a precomputed checksum of the data being sent, the same way as the BitTorrent protocol does.

xamgore
  • 1,723
  • 1
  • 15
  • 30