2

I have 2 UDP sockets (SOCKET), one for sending and one for receiving on a Windows machine. They both work well, but the trouble is that a program that is receiving messages from my send socket replies to the same port which sent the message.

I know that if I don't bind the send socket, using sendto will pick an ephemeral port to send on.

I'd like to know if it is possible to any of the following, and if so, what is the recommended way to do it:

  • Bind both the send and receive sockets to a chosen port so that when the external program sends a message back it can be received.
  • Update the port to which the receive socket is bound in such a way as to receive on the port from which I last sent a message (not sure if this would create a race condition).
  • Some other correct method.

So far I have tried:

  • Not binding the send socket (it sends from some open port to the destination port). I can successfully receive messages on that port for as long as it doesn't change, but eventually it does change.
  • Binding both the send and receive sockets to a desired port. This creates the desired behaviour when I watch the packets using a sniffer, but the receive socket never receives the messages even though I see them being transmitted to the correct port and IP.

Packets are being received from more than one outside entity, and not guaranteed to be in any particular order.

Thank you in advance!

user1205577
  • 2,388
  • 9
  • 35
  • 47
  • Yes but how would you use it to achieve the desired result? That is, how can you bind the send and receive sockets to the same port without conflict, and why? – user1205577 Jun 13 '12 at 17:50
  • 1
    Okay, a different question, what does sending on a separate socket buy you over sending over your receive socket? – jxh Jun 13 '12 at 18:14
  • Edited the original post to show which methods I tried and socket intent. – user1205577 Jun 13 '12 at 20:08
  • What's the problem with replying to the sending port? That is exactly what you want. I don't understand the question. – user207421 Jun 13 '12 at 22:12
  • Using a different socket buys you nothing except complication. The peer has to be rewritten to send to a fixed port number rather than the port the packet came from. There is no advantage at your end either. – user207421 Jun 14 '12 at 22:13

1 Answers1

1

Looks like you are trying to use threads to separate sending and receiving data. I would question this approach, since UDP is so easy to handle in one thread. Nevertheless, you can just use the same socket from both threads if you want (see related question: Are parallel calls to send/recv on the same socket valid?). Just bind(2) it and, optionally, connect(2) it.

Community
  • 1
  • 1
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • Thanks for the comment - if I send and receive on the same socket, won't it block until something is returned? I need to be able to send on demand and receive from multiple sources. – user1205577 Jun 13 '12 at 21:37
  • Then look into non-blocking sockets. That's the way to go when you have more then one. – Nikolai Fetissov Jun 13 '12 at 21:44