1
votes

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.

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)

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

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.

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

1

1 Answers

2
votes

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.