简体   繁体   中英

Storing TcpClient or NetworkStream for broadcast?

I'm storing TcpClient objects in a list, once client connects. When broadcasting to all connected clients, loop through the list of those and send data. The problem sometimes occurs while in a loop, throwing exception "Enumeration modified:" when one of those clients disconnects and its object is being disposed.

What would be a solution to that? Or simply store NetworkStream instead?

Thanks

You should use one of the ConcurrentCollections provided by .NET, eg ConcurrentBag or ConcurrentQueue.

See here: https://msdn.microsoft.com/en-us/library/dd997305%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM