简体   繁体   中英

How to send UDP datagrams from different applications but from the same SOURCE port?

We have two apps

  1. The first one is the UDP server and it listens and responds with the SOURCE port (12345)
  2. The second app is an only responds and no listen, we need to respond with a specified source port (12345)

How to achieve this?

We are trying to use many scenarios but:

  1. We can't bind the socket in the second app because it used in the first one
  2. We can't use SO_REUSEPORT in the second app for bind, because this app starts getting UDP data instead of the first app
  3. We can use IPC from the second app to the first app and then send as usual UDP (sendto) but this is a very huge workaround

One option would be for the server (1st process) to send the socket file descriptor to the sender (2nd process), so that they both share the same kernel UDP socket file description (rather then 2 sockets bound to the same port), but only one process (the server) reads from it.

You can pass a file descriptor to another process using either fork or fork-exec or Unix domain sockets SCM_RIGHTS ancillary messages .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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