I keep getting a client connection reset error. here is the code
#![allow(unused_imports)]
use std::{
io::{Read, Write},
net::TcpListener,
};
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 ms = [0u8; 4];
let _ = stream.read_exact(&mut ms).unwrap();
let len = i32::from_be_bytes(ms);
let mut buff = vec![0; (len - 4) as usize];
stream.read_exact(&mut buff).unwrap();
let _api = i16::from_be_bytes([buff[0], buff[1]]);
let _req_ver = i16::from_be_bytes([buff[2], buff[3]]);
let cid = [buff[4], buff[5], buff[6], buff[7]];
let error_code = 0i16.to_be_bytes();
let key_count = 1i32.to_be_bytes();
let api_key =
[18i16.to_be_bytes(), 0i16.to_be_bytes(), 4i16.to_be_bytes()].concat(); //is min 4 or zero?
let mess_len = cid.len()
+ error_code.len()
+ key_count.len()
+ api_key.len()
+ size_of::<i32>();
let mut resp = Vec::with_capacity(mess_len);
resp.extend((mess_len as i32).to_be_bytes());
resp.extend(cid);
resp.extend(error_code);
resp.extend(key_count);
resp.extend(api_key);
println!("{:?}", resp);
let _ = stream.write_all(&resp);
let res = stream.flush();
println!("res {:?}", res);
}
Err(e) => {
println!("error: {}", e);
}
}
}
}
Looking at the hexdump of the input and my output, I can’t see any issues. So is it connection handling?
[your_program] accepted new connection
[tester::#PV1] Sending "ApiVersions" (version: 4) request (Correlation id: 1163421261)
[tester::#PV1] Hexdump of sent "ApiVersions" request:
[tester::#PV1] Idx | Hex | ASCII
[tester::#PV1] -----+-------------------------------------------------+-----------------
[tester::#PV1] 0000 | 00 00 00 23 00 12 00 04 45 58 66 4d 00 09 6b 61 | ...#....EXfM..ka
[tester::#PV1] 0010 | 66 6b 61 2d 63 6c 69 00 0a 6b 61 66 6b 61 2d 63 | fka-cli..kafka-c
[tester::#PV1] 0020 | 6c 69 04 30 2e 31 00 | li.0.1.
[tester::#PV1]
[your_program] [0, 0, 0, 20, 69, 88, 102, 77, 0, 0, 0, 0, 0, 1, 0, 18, 0, 0, 0, 4]
[your_program] res Ok(())
[tester::#PV1] error reading from connection: read tcp 127.0.0.1:48012->127.0.0.1:9092: read: connection reset by peer
[tester::#PV1] Test failed
Do I need something for the tag_buffer?
Cheers!