首页 文章

UDP数据包到达Linux上的错误套接字

提问于
浏览
1

我有两个UDP套接字绑定到同一个地址并连接到地址A和B.我还有两个UDP套接字绑定到A和B并且没有连接 .

这就是我的 /proc/net/udp 看起来像(为了便于阅读而修剪):

sl  local_address rem_address
 3937: 0100007F:DD9C 0300007F:9910
 3937: 0100007F:DD9C 0200007F:907D
16962: 0200007F:907D 00000000:0000
19157: 0300007F:9910 00000000:0000

根据 connect(2) :“如果套接字sockfd的类型为SOCK_DGRAM,则addr是默认情况下发送数据报的地址, and the only address from which datagrams are received . ”

出于某种原因,我的连接套接字正在接收彼此发往的数据包 . 例如:连接到A的UDP套接字向A发送消息,A然后发回回 . 连接到B的UDP套接字向B发送消息,B然后发送回复 . 但是来自A的回复到达连接到B的套接字并且来自B的回复到达连接到A的套接字 .

为什么这会发生?请注意,它是随机发生的 - 有时回复到达正确的套接字,有时它们不会 . 有没有办法防止这种或任何情况下_2539143应该不起作用?

1 回答

  • 0

    嗯,据我所知,没有订购保证 .

    从手册页:

    SO_REUSEPORT (since Linux 3.9)
                  Permits multiple AF_INET or AF_INET6 sockets to be bound to an identical socket address.  This option must be set on each socket (including the first socket) prior to calling bind(2) on the socket.  To prevent port  hijacking,  all
                  of the processes binding to the same address must have the same effective UID.  This option can be employed with both TCP and UDP sockets.
    
                  For  TCP  sockets,  this  option allows accept(2) load distribution in a multi-threaded server to be improved by using a distinct listener socket for each thread.  This provides improved load distribution as compared to traditional
                  techniques such using a single accept(2)ing thread that distributes connections, or having multiple threads that compete to accept(2) from the same socket.
    
                  For UDP sockets, the use of this option can provide better distribution of incoming datagrams to multiple processes (or threads) as compared to the traditional technique of having multiple processes compete to receive datagrams  on
                  the same socket.
    

    所以你使用的东西主要被视为服务器的选项(或者在某些情况下是客户端,但是永远不能保证订购 - 特别是在UDP中)作为客户端 .

    我怀疑你的方法是错误的,需要重新思考=)

    PS . 刚看了一眼,但恕我直言,这是你的方法中的一个错误

相关问题