从 readLine() 方法收到的无限空值

Infinite nulls received from readLine() method

我制作了一个应用程序,它将加速度计坐标发送到我的 pc.The 代码,用于 android 端客户端是:

public class MainActivity extends Activity implements SensorEventListener{
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
float x ;
float y ;
float z;
TextView xt,yt,zt;
String st="left";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    senSensorManager.registerListener(this,senAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
    xt= (TextView) findViewById(R.id.xc);
    yt= (TextView) findViewById(R.id.yc);
    zt= (TextView) findViewById(R.id.zc);
    new SendMsg().execute();
}

public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
      x = sensorEvent.values[0];
      y = sensorEvent.values[1];
      z = sensorEvent.values[2];
        long curTime = System.currentTimeMillis();

        if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;
            xt.setText(String.valueOf(x));
            yt.setText(String.valueOf(y));
            zt.setText(String.valueOf(z));

         }
    }
}


public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
protected void onPause() {
    super.onPause();
    senSensorManager.unregisterListener( this);
}
protected void onResume() {
    super.onResume();
    senSensorManager.registerListener( this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

private class SendMsg extends AsyncTask<String, Void, String> {
    Socket s= null;
    OutputStream os;
    PrintWriter pw;
    @Override
    protected String doInBackground(String... params) {

        try {
            s = new Socket("192.168.0.102",2345);
            os=s.getOutputStream();
            pw=new PrintWriter(os);
            while(true){
                pw.println(x+" "+y+" "+z);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "executed";
    }
    protected void onPostExecute(String result) {

    }
}

PC端服务器代码为:

public static void main(String[] args) throws IOException, AWTException {
    ServerSocket ss = new ServerSocket(2345);
    Socket s = ss.accept();
    InputStream is = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    System.out.println("message received from server is :");
    for (;;) {
        String ms = br.readLine();
        System.out.println("" + ms);

    }

}

应用程序似乎没有发送加速度计坐标,而是发送了 NULL 并且在服务器端打印了同样的内容。

尽管我很努力,但还是没能弄清楚问题出在哪里。

你错了。 TCP 不能 'send null'。 readline()方法是返回null,*代替你接收数据。

原因很简单,readLine() returns 流末尾为 null,而您没有检查它。这在 Javadoc 中有明确说明。当它 returns null 时,对等方已关闭连接,因此您必须停止读取并通常关闭套接字。通常的写法是:

while ((line = br.readLine()) != null)
{
    // ...
}