Stuck trying to parse api version

#![allow(unused_imports)]
use std::{io::{Cursor, Read, Write}, net::TcpListener};
use tokio::io::AsyncReadExt;
#[tokio::main(flavor = "current_thread")]
async fn main() {
    
    println!("Logs from your program will appear here!");

    
    
    let listener = TcpListener::bind("127.0.0.1:9092").unwrap();
    
    for stream in listener.incoming() {
        match stream {
            Ok(mut _stream) => {

                
                println!("accepted new connection");

                let mut buf = [0_u8; 4];

                _stream.read_exact(&mut buf).unwrap();
                let mut rdr = Cursor::new(buf);
                let len = rdr.read_u32().await.unwrap();
                let mut msg = vec![0u8; len.try_into().unwrap()];
                _stream.read_exact(&mut msg.as_mut_slice()).unwrap();

                let mut rdr = Cursor::new(msg);

                rdr.read_u16().await.unwrap();
                let version = rdr.read_u16().await.unwrap();
                




                let cid = rdr.read_u32().await.unwrap();
                let acceptable_range = 0..4;
                if acceptable_range.contains(&version) {
                    _stream.write_all([0, 0, 0, 4].as_slice()).unwrap();
                    _stream.write_all(&cid.to_be_bytes()).unwrap();
                } else {

                    let errorcode: i32 = 35;
                    _stream.write_all([0, 0, 0, 4].as_slice()).unwrap();
                    _stream.write_all(&cid.to_be_bytes()).unwrap();
                    _stream.write_all(&errorcode.to_be_bytes()).unwrap();
                }
                
            }
            Err(e) => {
                println!("error: {}", e);
            }
        }
    }
}

this is my code
it says expected error 35 got 0

Hey @malawarecreator, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.