Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(async_await)]
- extern crate tokio;
- extern crate futures;
- //use crate::futures::executor::ThreadPool;
- use futures::task::SpawnExt;
- use tokio::net::{TcpListener, TcpStream};
- use std::{env, io, thread};
- use std::net::SocketAddr;
- use std::any::Any;
- use tokio::runtime::Runtime;
- use crate::tokio::io::AsyncWriteExt;
- use crate::tokio::io::AsyncReadExt;
- struct Client {
- socket: TcpStream,
- buf: Vec<u8>,
- }
- impl Client {
- async fn run(&mut self) {
- println!("{:?}", thread::current().name());
- loop {
- let mut a: [u8; 1024] = [0; 1024];
- let mut amt = self.socket.write(&a).await;
- match amt {
- Ok(_) => "",//println!("Echoed {} bytes", v),
- Err(e) => {
- println!("error : {:?}", e);
- break;
- }
- };
- amt = self.socket.read(&mut a).await;
- }
- }
- }
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn std::error::Error>> {
- // Bind the server's socket.
- // let mut rt = Runtime::new().unwrap();
- //let mut tp = ThreadPool::new().unwrap();
- let addr = "127.0.0.1:2794".parse().unwrap();
- let mut listener = TcpListener::bind(&addr)?;
- let a = async {
- loop {
- let (socket, addr) = listener.accept().await.unwrap();
- let mut client: Client = Client {
- socket,
- buf: vec![0; 1024],
- };
- tokio::spawn(async move {
- client.run().await;
- });
- println!("{}", addr);
- };
- };
- a.await;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement