How to get client connections on TCP Server with Tokio?

Dear community partners, I'm a newbie from C#.
When writing a simple rust TCP server with Tokio, a confused problem occured :
Find nowhere to get client sockets.
Knowing it's unpleasant to show C# code in Rust community, I have to offer a clear view to the
difference of socket processing between C# and Rust.

In C#, programmer could do this:
pseudocode

    main()
    {
        // A pool for storage of client connections
        var connectionPool = new List<connection>();

        // When client send something, CallBackMethod is invoked.
        server.connect.receivedRaw += CallBackMethod;
    }

    void CallBackMethod(Connection conn)
    {
        // Save connection to pool
        connectionPool.Add(conn);
     }

    bool SendToClient(string clientName, string message)
    {
        // Get client from pool
        var client = PickClientFromPool(clientName);
        client.Send(message);
    }

With a pool, one could send message to any active client in C#.
When come to Rust, I have complete no idea how to keep client connections...
There's tcp server tutorial by tokio offical site, as a echo server, it works really well.
But, how to move client connections to a connection pool for further usage?

let done = socket.incoming()
	.map_err(|e| println!("failed to accept socket; error = {:?}", e))
	.for_each(move |socket|{
		let (reader,writer) = socket.split();
		let amt = io::copy(reader, writer);

		let msg = amt.then(move |result|{
			match result {
				Ok((amt,_,_)) => println!("wrote {:?} bytes", amt),
				Err(e) => println!("error: {:?}", e),
			}

			Ok(())
		});

		tokio::spawn(msg)
	});

Errrr, I know it's a boring question.
Having look up answers for weeks, I still can't get any point to achieve my goal...
If there's any Lack of knowledge in my statement, please point out.
Any hint is welcomed.
Thanks.

I think this might be what you're looking for: https://tokio.rs/docs/going-deeper/chat/

They create the Shared struct which stores all of the connected peers. From there you can send to individual ones.

1 Like

Thanks a lot and ... yes it is!
A few days after asking this question, I have to struggle with it myself and find the same link to solve it finally.
:rofl: I was being absent from community for over half a month, it's really a pity for missing your reply.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.