networkingrustrust-tokiotcpserver

How to Check if Server has received data correctly


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?


Solution

  • 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.