Created
July 27, 2017 21:28
-
-
Save oremj/1d1a87ac9d3964215864b407c8e8a274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::env; | |
| use std::net::TcpStream; | |
| use std::thread; | |
| use std::time::Duration; | |
| const SLEEP_SECONDS: u64 = 604800; | |
| fn start_connections(conns: i32, addr: &str, start_port: u16, end_port: u16) -> thread::JoinHandle<()> { | |
| let addr = String::from(addr); | |
| thread::spawn(move || { | |
| println!("Starting thread"); | |
| let mut streams = Vec::new(); | |
| for _ in 0..conns { | |
| for port in start_port..end_port+1 { | |
| match TcpStream::connect((addr.as_str(), port)) { | |
| Ok(s) => streams.push(s), | |
| Err(e) => println!("Error connecting: {}", e), | |
| }; | |
| } | |
| } | |
| thread::sleep(Duration::from_secs(SLEEP_SECONDS)); | |
| }) | |
| } | |
| fn main() { | |
| let mut args = env::args(); | |
| let addr = match args.nth(1) { | |
| Some(x) => x, | |
| None => return usage(), | |
| }; | |
| let start_port: u16 = match args.next() { | |
| Some(x) => x.parse().expect("start_port must be int"), | |
| None => return usage(), | |
| }; | |
| let end_port: u16 = match args.next() { | |
| Some(x) => x.parse().expect("end_port must be int"), | |
| None => return usage(), | |
| }; | |
| let thread_cnt: i32 = args.next().expect("arg 2 must be thread count").parse().expect("arg 2 must be int"); | |
| let conns: i32 = match args.next() { | |
| Some(x) => x.parse().unwrap(), | |
| None => return usage(), | |
| }; | |
| let mut handles = vec![]; | |
| for _ in 0..thread_cnt { | |
| handles.push(start_connections(conns, &addr, start_port, end_port)); | |
| } | |
| for h in handles { | |
| h.join().unwrap(); | |
| } | |
| } | |
| fn usage() { | |
| let mut args = env::args(); | |
| println!("{} <addr> <start_port> <end_port> <threads> <connections per thread>", args.nth(0).unwrap()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::env; | |
| use std::net::TcpListener; | |
| use std::thread; | |
| fn listener(port: u16) -> thread::JoinHandle<()> { | |
| thread::spawn(move ||{ | |
| let listener = TcpListener::bind(("0.0.0.0", port)).expect("Failed to listen"); | |
| let mut connections = Vec::new(); | |
| loop { | |
| match listener.accept() { | |
| Ok((s, _)) => connections.push(s), | |
| Err(e) => println!("Failed to accept: {:?}", e), | |
| } | |
| } | |
| }) | |
| } | |
| fn main() { | |
| let mut args = env::args(); | |
| let start_port: u16 = match args.nth(1) { | |
| Some(x) => x.parse().expect("start_port must be int"), | |
| None => return usage(), | |
| }; | |
| let end_port: u16 = match args.next() { | |
| Some(x) => x.parse().expect("end_port must be int"), | |
| None => return usage(), | |
| }; | |
| let mut handles = vec![]; | |
| for port in start_port..(end_port+1) { | |
| handles.push(listener(port)); | |
| } | |
| for h in handles { | |
| h.join().unwrap(); | |
| } | |
| } | |
| fn usage() { | |
| let mut args = env::args(); | |
| println!("{} <start_port> <end_port>", args.nth(0).unwrap()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment