如何使用 unix Sockets API 查找本地可用的 UDP 端口
我有一个应用程序,其中可能有多个进程必须向一个进程发送消息。 我认为最方便的方法就是发送 UDP 数据报。
打开套接字时,我需要指定要侦听的端口。 因为我只想触发并忘记 UDP 数据报,所以我想让操作系统选择一个可用端口。 我宁愿不必寻找一个,特别是因为我的流程的许多实例可能正在运行。 有没有办法让操作系统只选择一个可用端口(或者更好的是,甚至不打开侦听套接字)来触发并忘记我的 UDP 数据报?
I have an application where I have potentially many processes must send a message to one process. I figured the most expedient way to do this would be to just send a UDP datagram.
When opening a socket, I need to specify which port to listen to. As I just want to fire and forget the UDP datagram, I'd like to just have the OS pick an available port. I'd rather not have to hunt for one, especially as many instances of my process may be running. Is there a way I can tell the OS to just pick an available port (or better yet, not even open a listening socket) to fire and forget my UDP datagram?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 指定 0 作为端口。 操作系统将为您选择一个可用的端口。
Yes. Specify 0 as the port. The OS will pick an available port for you.
回答“问题”而不是“问题”
如果所有进程都在同一台 PC 上运行,您可能需要研究共享文件或网络堆栈以外的其他通信方式。
我建议你探索 Boost 的 Interprocess 库中的选项
http://www.boost.org/doc/libs/ 1_37_0/doc/html/interprocess.html
快速指南:
http://www.boost.org/doc/ libs/1_37_0/doc/html/interprocess/quick_guide.html
具体来说,我建议您在进程之间使用命名管道或共享内存。
Answering the "Problem" rather than the "Question"
If all the processes are operating on the same PC, you might want to look into shared files or some other means of communications other than a networking stack.
I'd suggest you explore the options in Boost's Interprocess library
http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess.html
Quick guide here:
http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess/quick_guide.html
Specifically, I suggest you use a named pipe or shared memory between your processes.