Tcp peek size of message

    0

    0

    This code can be used to check the size of a message that is sent over a TCP stream. First, the code creates a buffer of 1024 bytes. Next, it uses the stream's Peek method to peek at the buffer, and then it checks whether the bytes_peeked value is equal to the buffer's length. If it is, the code prints out the bytes_peeked value and returns Ok(). If there is an error, the code prints out the error and returns Err().

    use std::error::Error;
    
    use tokio::net::TcpStream;
    
    pub async fn check_size_of_msg(stream: &mut TcpStream) -> Result<(), Box<dyn Error>> {
      let mut buffer = [0; 1024];
      let bytes_peeked = stream.peek(&mut buffer).await;
    
      match bytes_peeked {
        Ok(bytes_peeked) => {
          println!("bytes_peeked: {}", bytes_peeked);
          Ok(())
        },
        Err(e) => Err(e.into()),
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.