Java Socket Data收到无序

时间:2015-05-13 15:30:10

标签: java android

我正在从Android应用程序(Accelerometer和Gyro)收集数据并通过Java Socket将其发送到桌面应用程序,但速度很快 (SENSOR_DELAY_FASTEST,SENSOR_DELAY_GAME)

注意:

  • readingsList:包含传感器的所有读数
  • 我从列表中删除了发送到服务器的数据
  • 只发送一次数据,我确保从客户端订购 但是我把它们在服务器端无序(我收到了所有数据但没有订购)
  • processData(字符串读取)功能可能需要时间但不能太多

客户代码:

    class SocketClientThread implements Runnable {

    public SocketClientThread(){

    }
    public void run() {
        while (!Thread.currentThread().isInterrupted() && breathingStarted) {
            try {

                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);


                PrintWriter out =
                        new PrintWriter(socket.getOutputStream(), true);

                socket.setSendBufferSize(Integer.MAX_VALUE);
                socket.setReceiveBufferSize(Integer.MAX_VALUE);

                int lastCount = readingsList.size();

                out.println(readingsList); 
                out.flush();
                out.close(); 
                int toberemoved = lastCount;   
                if(readingsList.size() > 0){
                    for (int i = 0; i < toberemoved; i++) {
                        readingsList.remove(0);
                    }
                }


                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            } catch (UnknownHostException e1) {
                e1.printStackTrace();   
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }
}

服务器代码:

serverSocket = new ServerSocket(port);
    serverSocket.setSoTimeout(minutes*60*1000);

Thread thread = new Thread() {
    public void run() {
        while(!isStopped)
        {
            try
            {

                Socket connection = serverSocket.accept();
                connection.setReceiveBufferSize(Integer.MAX_VALUE);
                connection.setSendBufferSize(Integer.MAX_VALUE);


                BufferedReader input =
                        new BufferedReader(new InputStreamReader(connection.getInputStream()));


                PrintWriter out =
                        new PrintWriter(connection.getOutputStream(), true);

                String inputLine; 
                while ((inputLine = input.readLine()) != null) {
                    recentReading = inputLine; 
                    String oldReading = recentReading;
                    String modifiedReading = oldReading.replace("[", "");
                    modifiedReading = modifiedReading.replace("]", "");
                    String [] readings = modifiedReading.split(",");
                    for (int i = 0; i < readings.length; i++) {
                        String currentReading = readings[i].trim();
                        String [] tokens = currentReading.split("_");
                         processData(currentReading);


                    }
                }
            }catch(SocketTimeoutException s)
            {
                System.out.println("Socket timed out!");
                display.syncExec(new Runnable() {
                    public void run() {
                        statusVal.setText("Socket timed out!");
                    }
                });

                break;
            }catch(SocketException s)
            {
                if(serverSocket.isClosed()){
                    display.syncExec(new Runnable() {
                        public void run() {
                            statusVal.setText("Disconnected");
                        }
                    });
                }
                break;
            }catch(IOException e)
            {
                e.printStackTrace();
                break;
            }
        }

    }
};
thread.setDaemon(true);
thread.start();

我正在获取数据(未订购)

0 个答案:

没有答案