Unable to set DSCP value in Android application.

I am using android Studio to develop an application, and I want to set the DSCP value in the IP header using UDP sockets. I follow 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 searched on this forum and I found out that using System.setProperty("java.net.preferIPv4Stack", "true") , we can manipulate the DSCP values. But it doesn't seem to work on an Android app. How can I achieve the desired behavior? Am I missing something here? The code works without errors, but when I check the wiring (capturing the "any" interface and then applying the filter for udp) the value of the DSCP packet does not change. I am using an emulator on ubuntu 16 to test the script. Any help is appreciated.

+7
java android android-studio udp sockets
source share
1 answer

The above code works on an Android device (tested on Samsung's Galaxy S4), but not on an emulator. However, I could not install this System.setProperty("java.net.preferIPv4Stack", "true"); at runtime. DSCP can be set without explicitly setting this property in android. In addition to this, if you want to change the DSCP value in the IP header using a simple java program other than android, you can see this answer .

+1
source share

All Articles