通过单个函数调用写入多个文件描述符 [英] Writing to multiple file descriptors with a single function call

查看:122
本文介绍了通过单个函数调用写入多个文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于群聊服务器的用例,其中该服务器必须向所有客户端的套接字写入一个通用字符串.然后,我通过遍历文件描述符列表并将字符串写入每个文件描述符来解决此问题.

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.

现在,我正在考虑寻找一个更好的解决方案.是否可以通过使用Linux中的tee系统调用,通过服务器上的单个函数调用来做到这一点.我希望一个T恤的输出也可以转到下一个T恤,也要发送到客户端套接字.我想知道是否可以将tee一端的文件描述符复制到客户端套接字并获得所需的效果.

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.

谢谢

推荐答案

tee(2) 系统调用要求两个文件描述符都是管道-因此套接字不计算在内. splice(2)

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.

因此,您当前的围绕描述符的循环"与AFAICT差不多.

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

这篇关于通过单个函数调用写入多个文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆