2

I am developing an application for android where i need to communicate from server and i did that with the help of socket programming, now i need to find Packet Loss in my connection, so is there any API or any way to find Packet Loss in socket programming.

I am attaching my code snippet where client received message from server.

  try {
        socket = new Socket(dstAddress, dstPort);
        dataOutputStream = new DataOutputStream(
                socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());
        dataOutputStream.writeUTF(name);
        dataOutputStream.flush();

         String myArray = "";
         int i=0;
        while (!goOut) {
            flag=0;
            if (dataInputStream.available() > 0) {
                msgLog += dataInputStream.readUTF();
                flag=1; 
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        chatMsg.setText(msgLog);
                    }
                });

            Log.d("DERE", msgLog);

                 myArray=msgLog+"";   
              Log.v("ARE", myArray);
            }
            //Log.v("ARE", myArray[0]);
            if(flag==1)
            received(myArray);

            if(!msgToSend.equals("")){
                dataOutputStream.writeUTF(msgToSend);
                dataOutputStream.flush();
                msgToSend = "";
            }
        }
        //Looper.loop();

    } catch (UnknownHostException e) {
        e.printStackTrace();
        final String eString = e.toString();
        MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();
            }

        });
Tesy
  • 27
  • 1
  • 6
  • Socket class provides communication over TCP protocol which is secure and reliable. Thus you don't need to check packet loss in transportation level. If you want to check server responses in application level, you can set unique identifiers per request in order to keep track of server responses. – Efe Kahraman Feb 12 '15 at 06:20

2 Answers2

0

You can check this code.

String lost = new String();    
            String delay = new String();    
            Process p = Runtime.getRuntime().exec("ping -c 4 " + "119.147.15.13");    
            BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));    
            String str = new String();    
            while((str=buf.readLine())!=null){    
                if(str.contains("packet loss")){    
                    int i= str.indexOf("received");    
                    int j= str.indexOf("%");    
                    System.out.println(":"+str.substring(i+10, j+1));    
//                  System.out.println(":"+str.substring(j-3, j+1));     
                    lost = str.substring(i+10, j+1);    
                }    
                if(str.contains("avg")){    
                    int i=str.indexOf("/", 20);    
                    int j=str.indexOf(".", i);    
                    System.out.println(":"+str.substring(i+1, j));    
                    delay =str.substring(i+1, j);    
                    delay = delay+"ms";    
                }    

            }    

Source: http://www.phonesdevelopers.com/1762932/

  • thank for help and is there any way you know that we can measure packet loss without executing ping command?? – Tesy Feb 12 '15 at 06:38
  • actually i dont know how to measure get packet loss without ping. but seems like ping is best way. –  Feb 12 '15 at 09:47
0

Check below code.

public void ping()
{
String result = null;
   int pingCount=5;
   String[] rttPacket = new String[3];
   String[] packetLoss= new String[1];
    try {
        // url = "192.168.4.31";
        String pingCmd = "ping -c " + pingCount + " " + url;// url;//
        String pingResult = "";
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(pingCmd);
        BufferedReader in = new BufferedReader(new InputStreamReader(
            p.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            pingResult += inputLine;
        }
        in.close();
        ProcessSyncResponse(pingResult, url);
    }
   }    public String ProcessSyncResponse(String pingResult, String url) {
    String pingdataResult = null;
    try {
        int index2 = pingResult.indexOf("% packet loss");
        int index1 = pingResult.indexOf("rtt min/avg/max/mdev =");
        if (index1 > 0) {
            String parsedTime = (pingResult.substring(index1 + 23));
            rttPacket = parsedTime.split("/");
        } else {
            rttPacket[0] = "0";
            rttPacket[1] = "0";
            rttPacket[2] = "0";
        }
        if (index2 > 0) {
            String parsedPacket = "";
            int finalIndex = index2 + -3;
            String parsedintitalIndex = pingResult.substring(finalIndex,
                    index2);
            parsedintitalIndex = parsedintitalIndex.replaceAll(
                    "(,)|(,,)|( )", "");

            parsedPacket += parsedintitalIndex;
            parsedPacket += (pingResult.substring(index2));
            packetLoss = parsedPacket.split("%");

        } else {
            packetLoss[0] = "0";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
log.v(TAG," Packet Loss in %" +packetLoss[0]]);
log.v(TAG," Packet Loss AVG" +rttPacket[0]]);
log.v(TAG," Packet Loss Min" +rttPacket[1]]);
log.v(TAG," Packet Loss Max" +rttPacket[2]]); 

Glad if it worked for you.

Madhukar
  • 5
  • 6
  • thanks 4 the help it really help me alot and i am interested in to find packet loss while not using ping command so help me if u know any way to do this.. – Tesy Feb 12 '15 at 07:50
  • You can do it using ipconfig/ping. Other than that i have no idea.This could help you http://stackoverflow.com/questions/3211088/how-to-calculate-packet-loss-jitter-and-qos-parameters-of-video-over-an-ip-conn – Madhukar Feb 12 '15 at 12:49