People,
I am just getting started (again) and I have modified a working TcpListener program to allow for two different ports to be monitored but there are some bugs . . I copied most of the code out of the main module and created a new function so I could re-use code but I am obviously missing something. The code is below but when I run the program it exits instead of listening with this (inc debug statements):
$ ./t201_tcp_server
TcpListener { addr: V4(0.0.0.0:3333), fd: 3 }
Server listening on port 3333
Server listening on port 3334
In handle_listener
TcpListener { addr: V4(0.0.0.0:3333), fd: 3 }
Help appreciated!
Phil.
use std::thread;
use std::net::{TcpListener, TcpStream, Shutdown};
use std::io::{Read, Write};
fn handle_client(mut stream: TcpStream) {
println!("In handle_client") ;
println!("{:?}", stream) ;
let mut data = [0 as u8; 50]; // using 50 byte buffer
while match stream.read(&mut data) {
Ok(size) => {
// echo everything!
stream.write(&data[0..size]).unwrap();
true
},
Err(_) => {
println!("An error occurred, terminating connection with {}", stream.peer_addr().unwrap());
stream.shutdown(Shutdown::Both).unwrap();
false
}
} {}
}
fn handle_listener(listener: TcpListener) {
println!("In handle_listener") ;
println!("{:?}", listener) ;
for stream in listener.incoming() {
println!("New stream") ;
match stream {
Ok(stream) => {
println!("New connection: {}", stream.peer_addr().unwrap());
thread::spawn(move|| {
// connection succeeded
handle_client(stream)
});
}
Err(e) => {
println!("Error: {}", e);
/* connection failed */
}
}
}
}
fn main() {
let listener1 = TcpListener::bind("0.0.0.0:3333").unwrap();
// accept connections and process them, spawning a new thread for each one
println!("{:?}", listener1) ;
println!("Server listening on port 3333");
thread::spawn(move|| {
handle_listener(listener1)
});
let listener2 = TcpListener::bind("0.0.0.0:3334").unwrap();
// accept connections and process them, spawning a new thread for each one
println!("Server listening on port 3334");
thread::spawn(move|| {
handle_listener(listener2)
});
/*
// close the socket server
drop(listener1);
*/
}