pub fn send_bytes_to_server(stream: &mut TcpStream, bytes: &[u8]) -> Result<(), StreamError> {
    
    // ensure that the number of bytes is small enough
    if bytes.len() > MAX_N_BUFFERS * BUFFER_SIZE {
        return Err(StreamError { message: format!(
                    "Stream too long: size: {}, maximum size: {}",
                    bytes.len(), MAX_N_BUFFERS*BUFFER_SIZE
                   ) })
    }

    // the first bytes will determine the number of times the buffer should be read
    let mut n_buffers: u8 = (bytes.len() / BUFFER_SIZE) as u8;
    if bytes.len() % BUFFER_SIZE != 0 {
        n_buffers += 1;
    }
    stream.write(&[n_buffers])?;

    // write the data stream
    for i in 1..(n_buffers as usize) {
        stream.write(&bytes[(i-1)*BUFFER_SIZE..i*BUFFER_SIZE])?;
    }
    if n_buffers > 0 {
        stream.write(&bytes[((n_buffers-1) as usize)*BUFFER_SIZE..])?;
    }

    // wait for a reply to be sent from the receiver
    while let Err(_) = stream.read_exact(&mut [0]) {}
    
    Ok(())
}
/// send a sequence of bytes to the server and wait for confirmation that it has been received
send a sequence of bytes to the server and wait for confirmation that it has been received
send a sequence of bytes to the server and wait for confirmation that it has been received
pub fn send_bytes_to_server(stream: &mut TcpStream, bytes: &[u8]) -> Result<(), StreamError> {
    // ensure that the number of bytes is small enough
    if bytes.len() > MAX_N_BUFFERS * BUFFER_SIZE {
        return Err(StreamError { message: format!(
                    "Stream too long: size: {}, maximum size: {}",
                    bytes.len(), MAX_N_BUFFERS*BUFFER_SIZE
                   ) })
    }
    // the first bytes will determine the number of times the buffer should be read
    let mut n_buffers: u8 = (bytes.len() / BUFFER_SIZE) as u8;
    if bytes.len() % BUFFER_SIZE != 0 {
        n_buffers += 1;
    }
    stream.write(&[n_buffers])?;
    // write the data stream
    for i in 1..(n_buffers as usize) {
        stream.write(&bytes[(i-1)*BUFFER_SIZE..i*BUFFER_SIZE])?;
    }
    if n_buffers > 0 {
        stream.write(&bytes[((n_buffers-1) as usize)*BUFFER_SIZE..])?;
    }
    // wait for a reply to be sent from the receiver
    while let Err(_) = stream.read_exact(&mut [0]) {}
    Ok(())
}
pub fn send_bytes_to_server ( stream : & mut TcpStream , bytes : & [ u8 ] ) -> Result < ( ) , StreamError > { // ensure that the number of bytes is small enough if bytes . len ( ) > MAX_N_BUFFERS * BUFFER_SIZE { return Err ( StreamError { message : format ! ( " " bytes len ( ) MAX_N_BUFFERS BUFFER_SIZE ) } ) } // the first bytes will determine the number of times the buffer should be read let mut n_buffers : u8 = ( bytes . len ( ) / BUFFER_SIZE ) as u8 ; if bytes . len ( ) % BUFFER_SIZE != 0 { n_buffers += 1 ; } stream . write ( & [ n_buffers ] ) ? ; // write the data stream for i in 1 .. ( n_buffers as usize ) { stream . write ( & bytes [ ( i - 1 ) * BUFFER_SIZE .. i * BUFFER_SIZE ] ) ? ; } if n_buffers > 0 { stream . write ( & bytes [ ( ( n_buffers - 1 ) as usize ) * BUFFER_SIZE .. ] ) ? ; } // wait for a reply to be sent from the receiver while let Err ( _ ) = stream . read_exact ( & mut [ 0 ] ) { } Ok ( ( ) ) }
send a sequence of bytes to the server and wait for confirmation that it has been received
send a sequence of bytes to the server and wait for confirmation that it has been received
stream
&mut TcpStream
bytes
&[u8]
stream
&mut TcpStream
bytes
&[u8]