Unable to set DSCP value in android app

Viewed 1527

I am using android Studio for the app development and i want to set the DSCP value in the ip header using UDP sockets. I am following this example.

import android.os.Message;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpClientThread extends Thread{

    String dstAddress;
    int dstPort;
    private boolean running;
    MainActivity.UdpClientHandler handler;
    DatagramSocket socket;
    InetAddress address;

    public UdpClientThread(String addr, int port, MainActivity.UdpClientHandler handler) {
        super();
        dstAddress = addr;
        dstPort = port;
        this.handler = handler;

    }

    public void setRunning(boolean running){
        this.running = running;
    }

    private void sendState(String state){
        handler.sendMessage(
                Message.obtain(handler,
                        MainActivity.UdpClientHandler.UPDATE_STATE, state));
    }

    @Override
    public void run() {
        sendState("connecting...");

        running = true;
        System.setProperty("java.net.preferIPv4Stack", "true");


        try {
            socket = new DatagramSocket();
            socket.setTrafficClass(128); //Setting the DSCP value
            address = InetAddress.getByName(dstAddress);

            // send request
            byte[] buf = new byte[256];

            DatagramPacket packet =
                    new DatagramPacket(buf, buf.length, address, dstPort);


            socket.send(packet);

            sendState("connected");

            // get response
            packet = new DatagramPacket(buf, buf.length);


            socket.receive(packet);
            String line = new String(packet.getData(), 0, packet.getLength());

            handler.sendMessage(
                    Message.obtain(handler, MainActivity.UdpClientHandler.UPDATE_MSG, line));

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket != null){
                socket.close();
                handler.sendEmptyMessage(MainActivity.UdpClientHandler.UPDATE_END);
            }
        }

    }
}

I have searched on this forum and i came to know that using System.setProperty("java.net.preferIPv4Stack", "true") we can manipulate the DSCP values. But this seems not be working on the android app. How can i achieve the desired behavior? Am i missing something over here? The code works without any errors but when i check in the wireshark(capturing 'any' interface and then applying the filter for udp) the value of DSCP of the packet, it is unchanged. I am using Emulator on ubuntu 16 to to test the scenario. Any help is appreciated.

1 Answers
Related