Code example for DatagramPacket

Methods: getData, getLength, getOffset, getPort, setAddress, setPort

0
        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();
        InetAddress address = isNativeConnected ? null : packet.getAddress();
        netImpl.send(fd, packet.getData(), packet.getOffset(), packet.getLength(), port, address);
    } 
 
    public void setOption(int optID, Object val) throws SocketException {
        netImpl.setSocketOption(fd, optID, val);
    } 
 
    @Override 
    public void setTimeToLive(int ttl) throws IOException {
        setOption(IP_MULTICAST_TTL, Integer.valueOf(ttl));
    } 
 
    @Override 
    public void setTTL(byte ttl) throws IOException {
        setTimeToLive((int) ttl & 0xff); // Avoid sign extension.
    } 
 
    @Override 
    public void connect(InetAddress inetAddr, int port) throws SocketException {
        netImpl.connect(fd, inetAddr, port, 0);
 
        // if we get here then we are connected at the native level 
        try { 
            connectedAddress = InetAddress.getByAddress(inetAddr.getAddress());
        } catch (UnknownHostException e) {
            // this is never expected to happen as we should not have gotten 
            // here if the address is not resolvable 
            throw new SocketException("Host is unresolved: " + inetAddr.getHostName());
        } 
        connectedPort = port;
        isNativeConnected = true;
    } 
 
    @Override 
    public void disconnect() { 
        try { 
            netImpl.disconnectDatagram(fd);
        } catch (Exception e) {
            // there is currently no way to return an error so just eat any 
            // exception 
        } 
        connectedPort = -1;
        connectedAddress = null;
        isNativeConnected = false;
    } 
 
    /** 
     * Set the received address and port in the packet. We do this when the 
     * Datagram socket is connected at the native level and the 
     * recvConnnectedDatagramImpl does not update the packet with address from 
     * which the packet was received 
     * 
     * @param packet 
     *            the packet to be updated 
     */ 
    private void updatePacketRecvAddress(DatagramPacket packet) {
        packet.setAddress(connectedAddress);
        packet.setPort(connectedPort);
    } 
}