I used this code for the server, and modified this tutorial for the client code. When a client connects to the server, it blocks itself forever.
Server:
extern crate futures;
extern crate futures_io;
extern crate futures_mio;
use std::net::SocketAddr;
use futures::Future;
use futures_io::{copy, TaskIo};
use futures::stream::Stream;
fn main() {
let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let mut l = futures_mio::Loop::new().unwrap();
let server = l.handle().tcp_listen(&addr);
let done = server.and_then(move |socket| {
println!("Listening on: {}", addr);
socket.incoming().for_each(|(socket, addr)| {
let io = TaskIo::new(socket);
let pair = io.map(|io| io.split());
let amt = pair.and_then(|(reader, writer)| {
copy(reader, writer)
});
amt.map(move |amt| {
println!("wrote {} bytes to {}", amt, addr)
}).forget();
Ok(())
})
});
l.run(done).unwrap();
}
Client:
extern crate futures;
extern crate futures_io;
extern crate futures_mio;
use std::net::SocketAddr;
use futures::Future;
use futures_mio::Loop;
fn main() {
let mut lp = Loop::new().unwrap();
let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let socket = lp.handle().tcp_connect(&addr);
let request = socket.and_then(|socket| {
futures_io::write_all(socket, b"Hello!")
});
let response = request.and_then(|(socket, _)| {
futures_io::read_to_end(socket, Vec::new())
});
let data = lp.run(response).unwrap();
println!("{}", String::from_utf8_lossy(&data));
}