Writing to multiple file descriptors with a single

2019-05-23 00:00发布

I had a use case for a group chat server where the server had to write a common string to all clients' socket. I had then addressed this by looping through the list of file descriptors and writing the string to each of the file descriptors.

Now I am thinking of finding a better solution to the problem. Is it possible to do this by a single function call from the server by using the tee system call in linux. I want the output of one tee to go to the next tee as well to a clients socket. I am wondering if I can dup the file descriptor of one end of the tee to the clients socket and get the desired effect.

Please suggest any other implementation for the use case that you know of.

Thanks

1条回答
太酷不给撩
2楼-- · 2019-05-23 00:26

The tee(2) system call requires both file descriptors to be pipes - so sockets do not count. The splice(2) and vmsplice(2) system calls also do not seem to meet your requirements, and I don't see how to utilize sendfile(2) either.

I've not come across such a system call. Calls for collecting diverse data and writing it all at once (or the converse for reading) - yes. But for writing to multiple outputs at once - no.

So, your current 'loop around the descriptors' is about as good as it gets, AFAICT.

查看更多
登录 后发表回答