use of java.net.DatagramSocket in project mc-dev by Bukkit.
the class RemoteStatusListener method g.
private boolean g() {
try {
this.socket = new DatagramSocket(this.bindPort, InetAddress.getByName(this.motd));
this.a(this.socket);
this.socket.setSoTimeout(500);
return true;
} catch (SocketException socketexception) {
this.warning("Unable to initialise query system on " + this.motd + ":" + this.bindPort + " (Socket): " + socketexception.getMessage());
} catch (UnknownHostException unknownhostexception) {
this.warning("Unable to initialise query system on " + this.motd + ":" + this.bindPort + " (Unknown Host): " + unknownhostexception.getMessage());
} catch (Exception exception) {
this.warning("Unable to initialise query system on " + this.motd + ":" + this.bindPort + " (E): " + exception.getMessage());
}
return false;
}
use of java.net.DatagramSocket 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.DatagramSocket in project HdrHistogram by HdrHistogram.
the class SimpleHistogramExample method recordTimeToCreateAndCloseDatagramSocket.
static void recordTimeToCreateAndCloseDatagramSocket() {
long startTime = System.nanoTime();
try {
socket = new DatagramSocket();
} catch (SocketException ex) {
} finally {
socket.close();
}
long endTime = System.nanoTime();
histogram.recordValue(endTime - startTime);
}
use of java.net.DatagramSocket 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;
}
use of java.net.DatagramSocket in project opennms by OpenNMS.
the class NativeSocketTest method testServer.
@Test
public void testServer() throws Exception {
String[] cmds = new String[] { "echo", "echo2", "quit" };
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
for (final String cmd : cmds) {
final DatagramSocket sock = socket;
final FutureTask<DatagramPacket> task = new FutureTask<DatagramPacket>(new Callable<DatagramPacket>() {
@Override
public DatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final byte[] data = cmd.getBytes(StandardCharsets.UTF_8);
final DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), sock.getLocalPort());
sock.send(p);
printf("Receiving...\n");
final DatagramPacket r = new DatagramPacket(new byte[128], 128);
sock.receive(r);
printf("Received\n");
return r;
}
});
m_executor.execute(task);
final DatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = new String(r.getData(), r.getOffset(), r.getLength(), StandardCharsets.UTF_8);
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
} finally {
if (socket != null)
socket.close();
}
}
Aggregations