如何绑定2 UDP套接字同一端口,一个用于接收和其他用于发送

问题描述:

我在Android设备上,在那里我需要一个RTP流code工作的接受 RTP( UDP )端口上的数据包(比如5678)及发送 RTP从同一端口的数据包(5678)。
发件人code和接收器code是在不同的地点,因此,单个插座不能用于发送和接收。

I am working on a RTP streaming code on Android, where I need to receive RTP (UDP) packets on a port (say 5678) and send RTP packets from the same port (5678). Sender code and Receiver code are in separate locations, so a single socket cannot be used for both sending and receiving.

有关实现这一目标,我需要使用发件人插座和接收器插座绑定到同一端口的绑定()。只需2个插座上的bind(),我得到第2个绑定错误()。 (如预期)

For achieving this, I need to bind sender socket and receiver socket to same port using bind(). With just bind() on 2 sockets, I get error on 2nd bind(). (as expected)

在计算器上的其他职位做搜索之后,我发现使用套接字选项一个选项的 SO_REUSEADDR SO_REUSEPORT 不被用于Linux)。

After doing search on other posts on *, I found an option of using socket-option SO_REUSEADDR (SO_REUSEPORT not being available on Linux).

SO_REUSEADDR ,我能够绑定()两个插座到同一个端口。发件人插座能够从该端口的SendTo()。但是接收器插座停止显示读取任何可用的传入数据包(选择() + FD_ISSET()听力的方法)。如果bind()的编到另一端口,接收器插座接收另一个端口上接收的报文正常。

With SO_REUSEADDR, I am able to bind() two sockets to the same port. The sender socket is able to sendto() from this port. But the receiver socket stops showing any incoming packets available for reading (select() + FD_ISSET() method of listening). If bind()ed to other port, receiver socket receives the incoming packets on the other port normally.

那么有效,它只有一个插座能够使用共享端口,以及其他插座只是剥夺任何数据包的。

So effectively, its only one socket is able to use the shared port, and other socket is just deprived of any packets.

有关改进SO_REUSEADDR的方法或其他任何方式任何建议将高​​度AP preciated。

Any suggestions for improvements to SO_REUSEADDR approach or any other approach will be highly appreciated.

您不必在同一端口上绑定两个插槽。 Android的 的DatagramSocket 是bidrectional插座。简单地创建一个套接字,并使用其接收发送方法。你可以从不同的线程(接收发送),如果这是一个问题。

You don't need to bind two sockets on the same port. Android DatagramSocket is bidrectional socket. Simply create one socket and use its receive and send methods. You can use them from different threads (receive and send) if that's an issue.