简体   繁体   English

如何绑定/连接多个UDP套接字

[英]how to bind/connect multiple UDP socket

My initial UDP socket is binded to 127.0.0.1:9898. 我最初的UDP套接字已绑定到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). 第一次通过epoll / kqueue收到传入数据的通知时,我执行recvfrom()并填充了一个名为peer_name的结构sockaddr,其中包含对等信息(ip:port)。

Then I create a new UPD socket using socket(), 然后,我使用socket()创建一个新的UPD套接字,

then I bind() this newly created socket to the same ip:port (127.0.0.1:9898) than my original socket. 然后将这个新创建的套接字绑定()到与原始套接字相同的ip:port(127.0.0.1:9898)。

then I connect my newly created socket using connect() to the peer who just sent me something. 然后我使用connect()将新创建的套接字连接到刚给我发送消息的对等对象。 I have the information in the struct sockaddr called peer_name . 我在名为peer_name的struct sockaddr中具有信息。

I then add my newly created socket in my epoll/kqueue vector and wait for notification. 然后,将新创建的套接字添加到我的epoll / kqueue向量中,并等待通知。

I would expect to ONLY receive UDP frame from the peer i'm ""connected to"". 我希望只从“连接到”的对等方接收UDP帧。

1/ does netstat -a -p udp is suppose to show me the IP:PORT of the peer my newly created socket is ""connected to"" ? 1 / netstat -a -p udp是否可以向我显示我新创建的套接字的对等方的IP:PORT是““ 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. 2 /我可能做错了什么,因为创建新套接字后,此套接字将接收所有目的地为我绑定到的IP:PORT的传入UDP数据包,而不考虑源对等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! 谢谢!

http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-5.html 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. “执行connect()调用会影响套接字的接收行为吗?是的,有两种方式。首先,仅返回来自“已连接对等方”的数据报。所有到达您端口的其他数据报都不会传递给您。

But most importantly, a UDP socket must be connected to receive ICMP errors. 但最重要的是,必须连接UDP套接字才能接收ICMP错误。 Pp. Pp。 748-749 of "TCP/IP Illustrated, Volume 2" give all the gory details on why this is so." “ TCP / IP图解,第2卷”的748-749提供了有关为何如此的所有详细信息。”

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). UDP套接字上的connect(2)仅设置套接字的默认目标地址(如果在套接字上使用write(2)send(2) ,则将在其中发送数据)。 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. 它没有其他作用-您仍然可以使用sendto(2)sendmsg(2)将数据包发送到其他地址,并且仍然会看到从任何地址发送的数据包。

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). 因此,在端口上打开新的套接字实际上没有任何意义-对于收到的每个数据包,您需要查看源地址以查看其是否来自您已经看到的地址(因此属于该逻辑流)或是新地址(新逻辑流)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM