Example usage for java.net DatagramPacket getData

List of usage examples for java.net DatagramPacket getData

Introduction

In this page you can find the example usage for java.net DatagramPacket getData.

Prototype

public synchronized byte[] getData() 

Source Link

Document

Returns the data buffer.

Usage

From source file:ch.aschaefer.udp.DatagramSocketToControlMessageConverter.java

@Override
public ControlMessage convert(DatagramPacket source) {
    return new ControlMessage().timestamp(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now()))
            .hex(toHex(source.getData())).source(source.getSocketAddress().toString())
            .targetHost(source.getAddress().getHostAddress()).targetPort(source.getPort());
}

From source file:werewolf_client.Werewolf_Client.java

public void recvFromClient(String adr, int p) throws SocketException, IOException {
    DatagramSocket recv = new DatagramSocket(p);
    byte[] recvData = new byte[1024];

    while (true) {
        DatagramPacket recvPacket = new DatagramPacket(recvData, recvData.length);
        recv.receive(recvPacket);// w w w .  j  av  a 2s  .  c  o m

        String s = new String(recvPacket.getData(), 0, recvPacket.getLength());
        System.out.println("ReceivedD\n" + s);
    }
}

From source file:sx.blah.discord.api.internal.OpusPacket.java

OpusPacket(DatagramPacket udpPacket) {
    this.header = new RTPHeader(Arrays.copyOf(udpPacket.getData(), RTPHeader.LENGTH));
    this.audio = Arrays.copyOfRange(udpPacket.getData(), RTPHeader.LENGTH, udpPacket.getLength());
    this.isEncrypted = true;
}

From source file:org.graylog2.messagehandlers.gelf.GELFTest.java

/**
 * Test getGELFType method with a ZLIB encrypted message
 *///www  . j  a  v  a 2  s .  co  m
@Test
public void testGetGELFTypeWithZLIBEncryption() throws Exception {
    // Build a datagram packet.
    DatagramPacket gelfMessage = GELFTestHelper.buildZLIBCompressedDatagramPacket(this.originalMessage);

    assertEquals(GELF.TYPE_ZLIB, GELF.getGELFType(gelfMessage.getData()));
}

From source file:org.graylog2.messagehandlers.gelf.GELFTest.java

/**
 * Test getGELFType method with a GZIP encrypted message
 *///from w w w  . j  ava 2  s .  c  o  m
@Test
public void testGetGELFTypeWithGZIPEncryption() throws Exception {
    // Build a datagram packet.
    DatagramPacket gelfMessage = GELFTestHelper.buildGZIPCompressedDatagramPacket(this.originalMessage);

    assertEquals(GELF.TYPE_GZIP, GELF.getGELFType(gelfMessage.getData()));
}

From source file:org.openhab.binding.edimax.internal.UDPDiscoverer.java

private EdimaxDevice[] discover() throws SocketException, UnknownHostException, IOException {
    List<EdimaxDevice> discoveredDevices = new ArrayList<EdimaxDevice>();
    DatagramSocket serverSocket = null;
    try {//from   ww  w.  j a va 2s.c  o  m
        serverSocket = new DatagramSocket(12346); // choose random port,
        // because with empty
        // port sometimes error
        // occures.

        // send UDP broadcast
        InetAddress ipAddress = InetAddress.getByName("255.255.255.255");
        byte[] sendData = DISCOVERY_BYTES;
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddress, 20560);
        serverSocket.send(sendPacket);

        // receive
        serverSocket.setSoTimeout(1000 * 5);
        byte[] receiveData = new byte[1024];

        try {
            while (true) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

                serverSocket.receive(receivePacket);
                String sentence = new String(receivePacket.getData());

                if (!StringUtils.isEmpty(sentence) && sentence.contains("EDIMAX")) {
                    byte[] mac = new byte[6];
                    System.arraycopy(receivePacket.getData(), 0, mac, 0, 6);

                    String encodedMAC = Hex.encodeHexString(mac).toUpperCase();
                    InetAddress discoveredIp = receivePacket.getAddress();

                    EdimaxDevice dev = new EdimaxDevice();
                    dev.setIp(discoveredIp.getHostAddress());
                    dev.setMac(encodedMAC);

                    discoveredDevices.add(dev);
                }

            }
        } catch (SocketTimeoutException e) {
            // intended to happen
        }
    } finally {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }
    return discoveredDevices.toArray(new EdimaxDevice[discoveredDevices.size()]);
}

From source file:org.apache.camel.component.mina.MinaUdpWithInOutUsingPlainSocketTest.java

private String sendAndReceiveUdpMessages(String input) throws Exception {
    DatagramSocket socket = new DatagramSocket();
    InetAddress address = InetAddress.getByName("127.0.0.1");

    byte[] data = input.getBytes();

    DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT);
    LOG.debug("+++ Sending data +++");
    socket.send(packet);/*  w w w  .j a  va2 s .  co  m*/

    Thread.sleep(1000);

    byte[] buf = new byte[128];
    DatagramPacket receive = new DatagramPacket(buf, buf.length, address, PORT);
    LOG.debug("+++ Receving data +++");
    socket.receive(receive);

    socket.close();

    return new String(receive.getData(), 0, receive.getLength());
}

From source file:org.squidy.manager.protocol.udp.UDPServer.java

@Override
public void run() {
    while (running) {
        try {/*ww w .  j  a va  2 s  .  c o m*/
            byte[] buffer = new byte[BUFFER_SIZE];
            DatagramPacket packet = new DatagramPacket(buffer, BUFFER_SIZE);
            socket.receive(packet);
            data = new String(packet.getData());
            int last = 0;
            while (last < BUFFER_SIZE) {
                if (buffer[last] == 0)
                    break;
                last++;
            }
            data = data.substring(0, last);
            if (last == BUFFER_SIZE) {
                LOG.error("Input buffer overflow");
            } else {
                for (UDPListener listener : listeners) {
                    listener.parseData(data);
                }
            }

            for (UDPListener listener : listeners) {
                listener.parseData(packet.getData());
            }

            for (UDPListener listener : listeners) {
                listener.receive(packet);
            }
        } catch (IOException e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Connection error occured.");
            }
        }
    }
    socket.close();
}

From source file:org.apache.camel.component.netty.NettyUdpWithInOutUsingPlainSocketTest.java

private String sendAndReceiveUdpMessages(String input) throws Exception {
    DatagramSocket socket = new DatagramSocket();
    InetAddress address = InetAddress.getByName("127.0.0.1");

    // must append delimiter
    byte[] data = (input + "\n").getBytes();

    DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT);
    LOG.debug("+++ Sending data +++");
    socket.send(packet);/* w ww. j  a va  2s  . c om*/

    Thread.sleep(1000);

    byte[] buf = new byte[128];
    DatagramPacket receive = new DatagramPacket(buf, buf.length, address, PORT);
    LOG.debug("+++ Receiving data +++");
    socket.receive(receive);

    socket.close();

    return new String(receive.getData(), 0, receive.getLength());
}

From source file:org.dragonet.net.NetworkHandler.java

private void processPacket(DatagramPacket packet) {
    try {/*from w w w .j a v  a  2s.c  o  m*/
        PEBinaryReader reader = new PEBinaryReader(new ByteArrayInputStream(packet.getData()));
        int raknetPID = reader.readByte() & 0xFF;
        switch (raknetPID) {
        case RaknetConstants.ID_OPEN_CONNECTION_REQUEST_1:
            reader.read(16); //MAGIC
            reader.readByte(); //RakNet Protocol
            short mtu = (short) ((packet.getLength() - 18) & 0xFFFF);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PEBinaryWriter writer = new PEBinaryWriter(bos);
            writer.writeByte(RaknetConstants.ID_OPEN_CONNECTION_REPLY_1);
            writer.write(RaknetConstants.magic);
            writer.writeLong(NetworkHandler.serverID);
            writer.writeByte((byte) 0x00);
            writer.writeShort(mtu);
            this.udp.send(bos.toByteArray(), packet.getSocketAddress());
            break;
        case RaknetConstants.ID_OPEN_CONNECTION_REQUEST_2:
            reader.read(16);
            reader.read(5);
            reader.readShort();
            short clientMTU = reader.readShort();
            long clientID = reader.readLong();
            ByteArrayOutputStream bos8 = new ByteArrayOutputStream();
            PEBinaryWriter writer8 = new PEBinaryWriter(bos8);
            writer8.writeByte(RaknetConstants.ID_OPEN_CONNECTION_REPLY_2);
            writer8.write(RaknetConstants.magic);
            writer8.writeLong(NetworkHandler.serverID);
            writer8.writeShort((short) (packet.getPort() & 0xFFFF));
            writer8.writeShort(clientMTU);
            writer8.writeByte((byte) 0x00);
            this.send(bos8.toByteArray(), packet.getSocketAddress());
            DragonetSession session = new DragonetSession(this.server, packet.getSocketAddress(), clientID,
                    clientMTU);
            this.sessions.put(packet.getSocketAddress().toString(), session);
            //this.server.getServer().getSessionRegistry().add(session);
            break;
        case 0x80:
        case 0x81:
        case 0x82:
        case 0x83:
        case 0x84:
        case 0x85:
        case 0x86:
        case 0x87:
        case 0x88:
        case 0x89:
        case 0x8A:
        case 0x8B:
        case 0x8C:
        case 0x8D:
        case 0x8E:
        case 0x8F:
            if (this.sessions.containsKey(packet.getSocketAddress().toString())) {
                RaknetDataPacket dataPacket = new RaknetDataPacket(
                        ArrayUtils.subarray(packet.getData(), 1, packet.getLength()));
                dataPacket.decode();
                this.sessions.get(packet.getSocketAddress().toString()).processDataPacket(dataPacket);
            }
            break;
        }
    } catch (IOException e) {
    }
}