Robert Tupelo-Schneck
Robert Tupelo-Schneck

Reputation: 10543

How to deal with incorrect source address when using a DatagramSocket bound to the wildcard address?

I have a server with a DatagramSocket bound to the "ANY" wildcard address (using the DatagramSocket(int) constructor). This single socket allows the server to listen to UDP messages on multiple addresses, notably both IPv4 and IPv6.

Unfortunately, when sending the response, the server does not always choose as a source address the same address the client sent the request to; the result is an ICMPv6 message destination unreachable, unreachable port.

The same server also binds to the wildcard address for TCP, and never has the wrong source address issue with TCP. This Linux/C question seems to be about the same issue, and one answerer mentions that special handling is "not required for TCP because it handles multihoming transparently".

Is there any way to fix this problem in Java, other than binding a separate DatagramSocket for every address?

Upvotes: 0

Views: 308

Answers (2)

Warren Dew
Warren Dew

Reputation: 8938

The other solution, besides multiple DatagramSockets, would be to change the client code to listen to packets from any address, and include any necessary identifying information from the server in the packet payload.

However, I think having one DatagramSocket per server IP address would be a better solution. Using TCP would be better yet, as it sounds like you want a two way, connection oriented conversation, which is what TCP is designed for.

Upvotes: 0

John Hammond
John Hammond

Reputation: 1

The api for DatagramSocket says that the JVM has to match the OS. Also, the buffer size has to be big enough for queing all the ports.

Upvotes: 0

Related Questions