Reputation: 7394
I have this minimal implementation of a UDP client:
final class UdpClient {
private final DatagramSocket socket;
private final InetAddress address;
private final Integer port;
UdpClient(URI location) {
port = location.getPort();
try {
socket = new DatagramSocket();
address = InetAddress.getByName(location.getHost());
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException("Error setting up UDP connection", e);
}
}
void sendData(String msg) {
byte[] buf = msg.getBytes();
DatagramPacket packet
= new DatagramPacket(buf, buf.length, address, port);
try {
socket.send(packet);
} catch (IOException e) {
LOG.warn("Failed to send data {}", e.getMessage());
}
}
public void close() {
socket.close();
}
}
Now im starting to question if this is a valid impl since I never receive the reply. What are the consequences of me not caring about the reply? And is this even a valid impl without explicitly getting a reply?
The reason for my questions is that im worried that there might be some underlying buffer that is just filling up if I never do an explicit receive. This client could be used for a server that is sending a reply OR for a server that is not sending a reply.
Upvotes: 0
Views: 539
Reputation: 719446
Now I'm starting to question if this is a valid impl since I never receive the reply.
It is a valid implementation.
What are the consequences of me not caring about the reply?
The only real consequence is that the client won't be able to tell if the server it is sending packets is receiving them.
That may be acceptable to you.
And is this even a valid impl without explicitly getting a reply?
It is valid from the perspective of Java and the UDP specification.
Whether it makes sense to this ... we cannot judge ... because you have not explained why you are going to do this.
The reason for my questions is that I'm worried that there might be some underlying buffer that is just filling up if I never do an explicit receive.
There isn't ... in the sense that you mean. There may be an out-going packet buffer, and it may overflow, but that will only happen for some kinds of network link, and it won't be triggered by not receiving replies. (It might happen if the link is slow and can't send data fast enough. But if that happens, the client's OS should quietly drop packets out of the buffer. Your application won't know that is happening.)
However, there are some questionable aspects your implementation:
Problem #1
address = InetAddress.getByName(location.getHost());
You are ignoring the "protocol" of the URI. So for example, if the URI was an for resource hosted an "http" services, you would end up sending UDP packets to a host on (typically) port 80. But a web server does not listen for UDP packets on port 80. The chances are that the packets you send will be ignored.
Problem #2
throw new RuntimeException("Error setting up UDP connection", e);
UDP is connectionless, so you are not setting up a connection. Furthermore, no packets have been actually sent to the remote host at this point. The only thing that you have actually done is attempt to resolve the remote server's DNS name to an IP address.
Problem #3
LOG.warn("Failed to send data {}", e.getMessage());
It is true that you get to that point you have failed to send the packet. However, if you didn't get this exception, the only thing that you do know is that you have caused a packet to start its journey. You don't know if the packet has even made it off the client machine.
Upvotes: 1