Code example for DatagramPacket

Methods: getAddress, getData, getLength, getOffset, getPort

0
 
    @Override 
    protected int peek(InetAddress sender) throws IOException {
        // We don't actually want the data: we just want the DatagramPacket's filled-in address. 
        byte[] bytes = new byte[0];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        int result = peekData(packet);
        netImpl.setInetAddress(sender, packet.getAddress().getAddress());
        return result;
    } 
 
    private void doRecv(DatagramPacket pack, boolean peek) throws IOException {
        netImpl.recv(fd, pack, pack.getData(), pack.getOffset(), pack.getLength(), peek,
                isNativeConnected);
        if (isNativeConnected) {
            updatePacketRecvAddress(pack);
        } 
    } 
 
    @Override 
    public void receive(DatagramPacket pack) throws IOException {
        doRecv(pack, false);
    } 
 
    @Override 
    public int peekData(DatagramPacket pack) throws IOException {
        doRecv(pack, true);
        return pack.getPort();
    } 
 
    @Override 
    public void send(DatagramPacket packet) throws IOException {
        int port = isNativeConnected ? 0 : packet.getPort();