如何使用套接字编程来测量Android中的数据包丢失?

时间:2015-02-12 05:54:06

标签: java android sockets network-programming

我正在开发一个Android应用程序,我需要从服务器进行通信,我在套接字编程的帮助下做到了,现在我需要在我的连接中找到数据包丢失,所以有没有 API 或以任何方式查找套接字编程中的数据包丢失。

我正在附加我的代码段,客户端从服务器收到消息。

  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 个答案:

答案 0 :(得分:0)

您可以查看此代码。

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";    
                }    

            }    

来源:http://www.phonesdevelopers.com/1762932/

答案 1 :(得分:0)

检查以下代码。

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]]); 

很高兴,如果它适合你。