extern crate chain_p2p; use std::net::SocketAddr; use chain_p2p::p2p::P2PManager; #[test] fn server() { let mut manager = P2PManager::new(128); manager.server_run("127.0.0.1:8081"); // 子线程中循环执行 } fn server_write() { let mut manager = P2PManager::new(128); let addr = "127.0.0.1:8081".parse::().expect("Wrong host:port"); let msg = String::from("server write method test").into_bytes(); let n = manager.server_write(addr, msg); println!("write msg len: {:?}", n); } fn server_read() { let mut manager = P2PManager::new(128); let addr = "127.0.0.1:8081".parse::().expect("Wrong host:port"); let vec = manager.server_read(addr); println!("read bytes: {:?}", vec); } fn table() { let mut manager = P2PManager::new(128); let table = manager.server_table(); println!("table is {:?}", table); } fn client() { let mut manager = P2PManager::new(128); manager.create_session("127.0.0.1:8081").unwrap(); } fn client_read() { let mut manager = P2PManager::new(128); let vec = manager.client_read("127.0.0.1:8081"); println!("read bytes: {:?}", vec); } fn client_write() { let mut manager = P2PManager::new(128); let msg = String::from("server write method test").into_bytes(); let addr = "127.0.0.1:8081"; let n = manager.client_write(addr, msg); println!("write msg len: {:?}", n); } fn main() { server(); server_write(); server_read(); table(); client(); client_read(); client_write(); }