Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #![feature(async_await)]
  2. extern crate tokio;
  3. extern crate futures;
  4.  
  5. //use crate::futures::executor::ThreadPool;
  6.  
  7. use futures::task::SpawnExt;
  8. use tokio::net::{TcpListener, TcpStream};
  9. use std::{env, io, thread};
  10.  
  11. use std::net::SocketAddr;
  12. use std::any::Any;
  13. use tokio::runtime::Runtime;
  14. use crate::tokio::io::AsyncWriteExt;
  15. use crate::tokio::io::AsyncReadExt;
  16.  
  17. struct Client {
  18. socket: TcpStream,
  19. buf: Vec<u8>,
  20. }
  21.  
  22. impl Client {
  23. async fn run(&mut self) {
  24. println!("{:?}", thread::current().name());
  25. loop {
  26. let mut a: [u8; 1024] = [0; 1024];
  27. let mut amt = self.socket.write(&a).await;
  28.  
  29. match amt {
  30. Ok(_) => "",//println!("Echoed {} bytes", v),
  31. Err(e) => {
  32. println!("error : {:?}", e);
  33. break;
  34. }
  35. };
  36. amt = self.socket.read(&mut a).await;
  37. }
  38. }
  39. }
  40.  
  41.  
  42. #[tokio::main]
  43. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  44. // Bind the server's socket.
  45.  
  46. // let mut rt = Runtime::new().unwrap();
  47. //let mut tp = ThreadPool::new().unwrap();
  48. let addr = "127.0.0.1:2794".parse().unwrap();
  49. let mut listener = TcpListener::bind(&addr)?;
  50.  
  51. let a = async {
  52. loop {
  53. let (socket, addr) = listener.accept().await.unwrap();
  54. let mut client: Client = Client {
  55. socket,
  56. buf: vec![0; 1024],
  57. };
  58.  
  59.  
  60. tokio::spawn(async move {
  61. client.run().await;
  62. });
  63.  
  64.  
  65. println!("{}", addr);
  66. };
  67. };
  68. a.await;
  69. Ok(())
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement