Search in sources :

Example 76 with DatagramSocket

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;
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException)

Example 77 with DatagramSocket

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();
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Example 78 with DatagramSocket

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);
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket)

Example 79 with DatagramSocket

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;
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 80 with DatagramSocket

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();
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) Test(org.junit.Test)

Aggregations

DatagramSocket (java.net.DatagramSocket)294 DatagramPacket (java.net.DatagramPacket)137 IOException (java.io.IOException)112 SocketException (java.net.SocketException)74 InetAddress (java.net.InetAddress)63 InetSocketAddress (java.net.InetSocketAddress)46 UnknownHostException (java.net.UnknownHostException)33 Test (org.junit.Test)27 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 ServerSocket (java.net.ServerSocket)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 DatagramChannel (java.nio.channels.DatagramChannel)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)9 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)6