org.xbill.DNS.UDPClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dnsjava-osgi Show documentation
Show all versions of dnsjava-osgi Show documentation
Repackaging dnsjava 2.0.6 for OSGI and Maven environments.
The newest version!
// Copyright (c) 2005 Brian Wellington ([email protected])
package org.xbill.DNS;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
final class UDPClient extends Client {
public
UDPClient(long endTime) throws IOException {
super(DatagramChannel.open(), endTime);
}
void
bind(SocketAddress addr) throws IOException {
DatagramChannel channel = (DatagramChannel) key.channel();
channel.socket().bind(addr);
}
void
connect(SocketAddress addr) throws IOException {
DatagramChannel channel = (DatagramChannel) key.channel();
channel.connect(addr);
}
void
send(byte [] data) throws IOException {
DatagramChannel channel = (DatagramChannel) key.channel();
verboseLog("UDP write", data);
channel.write(ByteBuffer.wrap(data));
}
byte []
recv(int max) throws IOException {
DatagramChannel channel = (DatagramChannel) key.channel();
byte [] temp = new byte[max];
key.interestOps(SelectionKey.OP_READ);
try {
while (!key.isReadable())
blockUntil(key, endTime);
}
finally {
if (key.isValid())
key.interestOps(0);
}
long ret = channel.read(ByteBuffer.wrap(temp));
if (ret <= 0)
throw new EOFException();
int len = (int) ret;
byte [] data = new byte[len];
System.arraycopy(temp, 0, data, 0, len);
verboseLog("UDP read", data);
return data;
}
static byte []
sendrecv(SocketAddress local, SocketAddress remote, byte [] data, int max,
long endTime)
throws IOException
{
UDPClient client = new UDPClient(endTime);
try {
if (local != null)
client.bind(local);
client.connect(remote);
client.send(data);
return client.recv(max);
}
finally {
client.cleanup();
}
}
static byte []
sendrecv(SocketAddress addr, byte [] data, int max, long endTime)
throws IOException
{
return sendrecv(null, addr, data, max, endTime);
}
}