How can I maintain a Vec of net::TcpStream?

I am trying to write an echo serverwhere instead of just a single client the mesage gets echoed to all the clients. I guess it can be called like broadcasting. Problem is I am a noob both in rust and network programming, and I have only used JS and Python untill now. So I have no idea what is it I want and what I need to do to achieve that. Any help in the right direction will be appreciated. Here's a link to my question on Stack Overflow.

1 Like

Looks like you got a good answer on SO. If you want to see how this might look like using Tokio, take a look at https://github.com/arjsin/tokio-broadcast-example.

2 Likes

Yep. And thanks. :smiley: I have been actually avoiding tokio or any other abstractions to see if I can implement it myself.

1 Like

Ok, that's a good learning exercise :+1:. One thing I didn't see mentioned in the SO thread is blocking. It looks like the example code there is using blocking read/write socket calls. If you broadcast a message to other threads, they won't notice until their read/write calls return, and that's dependent on the peer. So at a minimum, you'll want to make the sockets use non-blocking I/O, at which point you need to maintain a state machine of read/write activity.

You can skip Tokio and use mio instead to at least get some basic non-blocking/readiness based socket abstraction. Unless you want to implement that yourself as well :slight_smile:.

1 Like

I am just taking a look at mio. Thanks. Thing is I am a noob, so I still don't know much about the rust ecosystem. :stuck_out_tongue:

1 Like

That's completely fine - you've come to the right place to get help :slight_smile:.

2 Likes