多个 UDP 套接字侦听同一端口上的特定源 [英] Multiple UDP Sockets to listen for specific source on the same port

查看:54
本文介绍了多个 UDP 套接字侦听同一端口上的特定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然可以构造一个监听同一个端口的tcp socket和一个udp socket,那么是否可以构造多个监听同一端口上的udp消息的socket,每个socket监听不同的源端点?

Since it is possible to construct a tcp socket and a udp socket that listens on the same port, is it possible to construct multiple sockets that listen for udp messages on the same port, with each listening for different source endpoints?

在设计方面,我目前的印象是,正确"的做法是让单个套接字侦听所有 udp 消息,并让它在应用程序级别确定来源.

Design-wise, I'm currently under the impression that the 'correct' way to do it is just to have a single socket listening for all udp messages and have it determine the origin at the application level.

推荐答案

通过使用 SO_REUSEADDR 套接字选项,可以让多个 UDP 套接字监听同一个本地 IP/端口对,但是没有无法告诉每个套接字仅接受其预期远程 IP/端口对的数据包,因此无法保证哪个套接字将接收哪个数据包.所以是的,正确的选择是使用单个套接字来查看每个接收到的数据包的发送 IP/端口并相应地引导数据包.

It is possible to have multiple UDP sockets listening on the same local IP/Port pair by using the SO_REUSEADDR socket option, but there is no way to tell each socket to only accept packets for its intended remote IP/Port pair, so there is no guarantee which socket will receive which packet. So yes, the correct option is to use a single socket that looks at the sending IP/Port of each received packet and directs the packet accordingly.

这篇关于多个 UDP 套接字侦听同一端口上的特定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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