2
votes

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();
            }

        });
2
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

2 Answers

0
votes

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/

0
votes

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.