Tokio-core, futures, receive and send in any order problem

Hi,
I try to use tokio-core 0.1 library to send and receive some message over tcp socket, but I have problem with understanding futures and get program compiled.
Here is my code:

fn new_connection(socket: TcpStream, client_address: std::net::SocketAddr) {
        let b = generate_public();
        let (to_client, from_client) = socket.framed(ServerPublicNumberCodec::new()).split();

        let send_server_number = to_client
            .send(ServerPublicNumber::new(b));

        let read_client_number = from_client
            .into_future()
            .and_then(move |number_msg| {
                Ok(number_msg)
            });

        let joined = send_server_number.join(read_client_number);
        joined.poll();
}

I try to get at the end future which I could poll. I would like this to work with sending and receiving in any order.

while compiling I get an error:

error[E0271]: type mismatch resolving `<futures::AndThen<futures::stream::StreamFuture<futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>>, std::result::Result<(std::option::Option<common::ClientPublicNumber>, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>), (std::io::Error, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>)>, [closure@src/main.rs:83:23: 85:14]> as futures::IntoFuture>::Error == std::io::Error`
  --> src/main.rs:87:41
   |
87 |         let joined = send_server_number.join(read_client_number);
   |                                         ^^^^ expected tuple, found struct `std::io::Error`
   |
   = note: expected type `(std::io::Error, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>)`
              found type `std::io::Error`

error[E0271]: type mismatch resolving `<futures::AndThen<futures::stream::StreamFuture<futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>>, std::result::Result<(std::option::Option<common::ClientPublicNumber>, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>), (std::io::Error, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>)>, [closure@src/main.rs:83:23: 85:14]> as futures::Future>::Error == std::io::Error`
  --> src/main.rs:87:41
   |
87 |         let joined = send_server_number.join(read_client_number);
   |                                         ^^^^ expected tuple, found struct `std::io::Error`
   |
   = note: expected type `(std::io::Error, futures::stream::SplitStream<tokio_core::io::Framed<tokio_core::net::TcpStream, common::codec::LengthPrefixedJson<common::ClientPublicNumber, common::ServerPublicNumber>>>)`
              found type `std::io::Error`
   = note: required by `futures::Join`

The immediate (i.e. compilation) issue is that Join (returned by join()) implements Future only when both joined futures have the same Error type. send_server_number has Error = std::io::Error, but read_client_number has Error = (std::io::Error, SplitStream<...>) - i.e. it's a tuple (this is how into_future works on a Stream).

The second issue is you're trying to poll() the future - you generally do not do this; polling futures is done by the reactor/executor, tokio in your case. What you probably want to do is spawn() this joined future onto the reactor, although I'm not sure what exactly you're trying to achieve here. What do you want to happen when both futures are done?

Here is a rough sketch of your code on the playground. It compiles, but don't try to run it of course. Perhaps we can use that to continue this exercise, if you'd like to expand on your use case a bit more.

Also, consider switching to tokio from tokio-core - the latter is, essentially, deprecated.

Thanks for response, it helped a lot.

Generally I am trying to make simple server-client encrypted chat. I do it on the basis of one of the very few examples ( tokio-chat-example/main.rs at master · jgallagher/tokio-chat-example · GitHub ) which uses tokio-core.
Later I wanted to chain this future further and spawn it in the proper tokio way, but that was not the thing I wanted to focus on.
I think I will start over again with new tokio. There is to many things I understand poorly so it is better for me to use not deprecated libs.
Thanks for help.