Search in sources :

Example 86 with DatagramPacket

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;*/
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) ByteBuffer(java.nio.ByteBuffer)

Example 87 with DatagramPacket

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

Example 88 with DatagramPacket

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

Example 89 with DatagramPacket

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

Example 90 with DatagramPacket

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

Aggregations

DatagramPacket (java.net.DatagramPacket)334 DatagramSocket (java.net.DatagramSocket)138 IOException (java.io.IOException)137 InetAddress (java.net.InetAddress)77 SocketException (java.net.SocketException)48 Test (org.junit.Test)37 InetSocketAddress (java.net.InetSocketAddress)34 SocketTimeoutException (java.net.SocketTimeoutException)30 UnknownHostException (java.net.UnknownHostException)26 MulticastSocket (java.net.MulticastSocket)19 InterruptedIOException (java.io.InterruptedIOException)17 SocketAddress (java.net.SocketAddress)16 ByteBuffer (java.nio.ByteBuffer)11 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)11 ArrayList (java.util.ArrayList)10 PortUnreachableException (java.net.PortUnreachableException)9 SyslogMessageLogDTO (org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO)8 BindException (java.net.BindException)7 Transactional (org.springframework.transaction.annotation.Transactional)7