use of java.net.DatagramPacket in project Dragonet-Legacy by DragonetMC.
the class UDPServerSocket method readPacket.
public DatagramPacket readPacket() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(65536);
InetSocketAddress socketAddress = (InetSocketAddress) this.channel.receive(buffer);
if (socketAddress == null) {
return null;
}
DatagramPacket packet = new DatagramPacket(new byte[buffer.position()], buffer.position());
packet.setAddress(socketAddress.getAddress());
packet.setPort(socketAddress.getPort());
packet.setLength(buffer.position());
packet.setData(Arrays.copyOf(buffer.array(), packet.getLength()));
//MainLogger.getLogger().debug(TextFormat.YELLOW + "In: " + Binary.bytesToHexString(packet.getData(), true));
return packet;
/*DatagramPacket packet = new DatagramPacket(new byte[65536], 65536);
this.socket.receive(packet);
packet.setData(Arrays.copyOf(packet.getData(), packet.getLength()));
return packet;*/
}
use of java.net.DatagramPacket in project graylog2-server by Graylog2.
the class UdpTransportTest method sendUdpDatagram.
private void sendUdpDatagram(String hostname, int port, int size) throws IOException {
final InetAddress address = InetAddress.getByName(hostname);
final byte[] data = new byte[size];
final DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
DatagramSocket toSocket = null;
try {
toSocket = new DatagramSocket();
toSocket.send(packet);
} finally {
if (toSocket != null) {
toSocket.close();
}
}
}
use of java.net.DatagramPacket in project GNS by MobilityFirst.
the class LookupWorker method sendResponse.
/**
* Returns a response to the sender.
*
* @param responseBytes
*/
private void sendResponse(byte[] responseBytes) {
DatagramPacket outgoingPacket = new DatagramPacket(responseBytes, responseBytes.length, incomingPacket.getAddress(), incomingPacket.getPort());
try {
socket.send(outgoingPacket);
NameResolution.getLogger().log(Level.FINE, "Response sent to {0} {1}", new Object[] { incomingPacket.getAddress().toString(), incomingPacket.getPort() });
} catch (IOException e) {
NameResolution.getLogger().log(Level.SEVERE, "Failed to send response{0}", e);
}
}
use of java.net.DatagramPacket in project GNS by MobilityFirst.
the class UdpDnsServer method run.
@Override
public void run() {
NameResolution.getLogger().log(Level.INFO, "Starting local DNS Server on port {0}{1}fallback DNS server at {2}", new Object[] { sock.getLocalPort(), gnsServerIP != null ? (" with GNS server at " + gnsServerIP + " and ") : " with ", dnsServerIP });
while (true) {
try {
final short udpLength = 512;
while (true) {
byte[] incomingData = new byte[udpLength];
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
// Read the incoming request
incomingPacket.setLength(incomingData.length);
try {
sock.receive(incomingPacket);
} catch (InterruptedIOException e) {
continue;
}
executor.execute(new LookupWorker(sock, incomingPacket, incomingData, gnsServer, dnsServer, dnsCache, handler));
}
} catch (IOException e) {
NameResolution.getLogger().log(Level.SEVERE, "Error in UDP Server (will sleep for 3 seconds and try again): {0}", e);
ThreadUtils.sleep(3000);
}
}
}
use of java.net.DatagramPacket in project android_frameworks_base by DirtyUnicorns.
the class BandwidthEnforcementTestService method testDns.
/**
* Tests dns query.
* @return true if it was able to connect, false otherwise.
*/
public static boolean testDns() {
try {
final DatagramSocket socket = new DatagramSocket();
try {
socket.setSoTimeout(10000);
final byte[] query = buildDnsQuery("www", "android", "com");
final DatagramPacket queryPacket = new DatagramPacket(query, query.length, InetAddress.parseNumericAddress("8.8.8.8"), 53);
socket.send(queryPacket);
final byte[] reply = new byte[query.length];
final DatagramPacket replyPacket = new DatagramPacket(reply, reply.length);
socket.receive(replyPacket);
return true;
} finally {
socket.close();
}
} catch (IOException e) {
Log.d(TAG, "error: " + e);
}
return false;
}
Aggregations