🚧 wip: little TCP server
							parent
							
								
									cfae81c780
								
							
						
					
					
						commit
						9405a2d10b
					
				
							
								
								
									
										36
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										36
									
								
								src/main.rs
								
								
								
								
							|  | @ -48,6 +48,7 @@ fn main() -> Result <(), Error> | |||
|             
 | ||||
|             println! ("{} ms", (stop - start).as_millis ()); | ||||
|         }, | ||||
|         Some ("network") => main_network (), | ||||
|         Some (_) => eprintln! ("Unknown subcommand"), | ||||
|     } | ||||
|     
 | ||||
|  | @ -199,6 +200,41 @@ fn main_egui () | |||
|     eframe::run_native("Five Five Five", native_options, Box::new(|cc| Box::new(App::new(cc, send_to_ctl, recv_at_gui, send_gui_handle)))).unwrap (); | ||||
| } | ||||
| 
 | ||||
| fn main_network () | ||||
| { | ||||
|     use tokio::io:: | ||||
|     { | ||||
|         AsyncReadExt, | ||||
|         AsyncWriteExt, | ||||
|     }; | ||||
|     
 | ||||
|     let rt = tokio::runtime::Runtime::new ().unwrap (); | ||||
|     
 | ||||
|     rt.block_on (async 
 | ||||
|     { | ||||
|         let server = tokio::net::TcpListener::bind ("127.0.0.1:9001").await.unwrap (); | ||||
|         
 | ||||
|         tokio::spawn (async move | ||||
|         { | ||||
|             while let Ok ((mut stream, _remote_addr)) = server.accept ().await | ||||
|             { | ||||
|                 tokio::spawn (async move | ||||
|                 { | ||||
|                     for i in 0.. 
 | ||||
|                     { | ||||
|                         let mut buf = vec! [0u8]; | ||||
|                         if let Err (_) = stream.read_exact (&mut buf).await | ||||
|                         { | ||||
|                             break; | ||||
|                         } | ||||
|                         stream.write_all (format! ("{i}\n").as_bytes ()).await.unwrap (); | ||||
|                     } | ||||
|                 }); | ||||
|             } | ||||
|         }).await.unwrap (); | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| #[derive (Debug, thiserror::Error)] | ||||
| enum Error | ||||
| { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 _
						_