如何绑定/连接多个UDP套接字
我的初始 UDP 套接字绑定到 127.0.0.1:9898。
当我第一次收到 epoll/kqueue 传入数据的通知时,我执行 recvfrom() 并填充一个名为 peer_name
的 struct sockaddr,其中包含对等信息(ip:port)。
然后我使用 socket() 创建一个新的 UPD 套接字,
然后将这个新创建的套接字绑定() 到与原始套接字相同的 ip:port (127.0.0.1:9898)。
然后我使用 connect() 将新创建的套接字连接到刚刚向我发送内容的对等方。我在名为 peer_name
的 struct sockaddr 中有信息。
然后,我将新创建的套接字添加到 epoll/kqueue 向量中并等待通知。
我希望只从我“连接到”的对等方接收 UDP 帧。
1/netstat -a -p udp 是否应该向我显示我新创建的套接字“连接到”的对等点的 IP:PORT?
2/ 我可能做错了什么,因为在创建新套接字后,该套接字接收所有发往我绑定的 IP:PORT 的传入 UDP 数据包,无论源对等 IP:PORT 是什么。
我想看看我正在尝试做的事情的工作示例:) 或任何关于我做错了什么的暗示。
谢谢!
My initial UDP socket is binded to 127.0.0.1:9898.
The first time that I get notified of incoming data by epoll/kqueue, I do recvfrom() and I fill a struct sockaddr called peer_name
that contain the peer informations (ip:port).
Then I create a new UPD socket using socket(),
then I bind() this newly created socket to the same ip:port (127.0.0.1:9898) than my original socket.
then I connect my newly created socket using connect() to the peer who just sent me something. I have the information in the struct sockaddr called peer_name
.
I then add my newly created socket in my epoll/kqueue vector and wait for notification.
I would expect to ONLY receive UDP frame from the peer i'm ""connected to"".
1/ does netstat -a -p udp
is suppose to show me the IP:PORT of the peer my newly created socket is ""connected to"" ?
2/ I'm probably doing something wrong since after creating my new socket, this socket receive all incoming UDP packets destinated to the IP:PORT I'm binded to, regardless of the source peer IP:PORT.
I would like to see a working example of what I'm trying to do :)
or any hint on what I'm doing wrong.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www .softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-5.html
“执行 connect() 调用是否会影响套接字的接收行为?
是的,有两种方式。首先,仅返回来自“连接的对等点”的数据报。所有到达您港口的其他货物都不会交付给您。
但最重要的是,必须连接 UDP 套接字才能接收 ICMP 错误。页码。 《TCP/IP 图解,第 2 卷》的第 748-749 节给出了为什么会出现这种情况的所有血淋淋的细节。
http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-5.html
"Does doing a connect() call affect the receive behaviourof the socket?
Yes, in two ways. First, only datagrams from your "connected peer" are returned. All others arriving at your port are not delivered to you.
But most importantly, a UDP socket must be connected to receive ICMP errors. Pp. 748-749 of "TCP/IP Illustrated, Volume 2" give all the gory details on why this is so."
UDP 套接字上的 connect(2) 仅设置套接字的默认目标地址(如果使用 write(2) 或 send 则数据将发送到该地址(2) 在插座上)。它没有其他影响 - 您仍然可以使用 sendto(2) 或 sendmsg(2) 将数据包发送到其他地址,并且您仍然会看到从任何地址发送的数据包。
因此,在端口上打开一个新套接字实际上没有任何意义——对于收到的每个数据包,您需要查看源地址以查看它是否来自您已经见过的地址(因此属于该逻辑流)或者是一个新地址(一个新逻辑流)。
connect(2) on a UDP socket just sets the default destination address of the socket (where the data will be sent if you use write(2) or send(2) on the socket). It has no other effect -- you can still send packets to other addresses with sendto(2) or sendmsg(2) and you'll still see packets sent from any address.
So it doesn't really make any sense to open a new socket on the port -- for every packet received, you need to look at the source address to see if it comes from an address you've seen already (and thus belongs to that logical stream) or is a new address (a new logical stream).