0

I'm trying to sent a simple message through TCP from android device to a computer. When I try to run this code I get this error:

'java.net.SocketException: socket failed: EACCES (Permission denied)'

Code

public class MainActivity extends Activity implements SensorEventListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        b = (Button)findViewById(R.id.button2);
        err = (TextView)findViewById(R.id.errors);
        mSensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
        mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        mSensorManager.registerListener(this, mRotationSensor, SENSOR_DELAY);
    } catch (Exception e) {
        Toast.makeText(this, "Hardware compatibility issue", Toast.LENGTH_LONG).show();
    }
    b.setOnClickListener(new View.OnClickListener() {
        public  void onClick(View v){
            try{

                Socket s = new Socket("10.0.0.7", 5555);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                String outMsg = "";
                outMsg = "This is Client";
                out.write(outMsg);
                out.flush();
                out.close();
                s.close();
                err.setText("sending");
            }

            catch(Exception ex){
                err.setText("error"+ex.toString());

            }
        }
    });
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mRotationSensor) {
        if (event.values.length > 4) {
            float[] truncatedRotationVector = new float[4];
            System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
            update(truncatedRotationVector);
        } else {
            update(event.values);
        }
    }
}
private void update(float[] vectors) {
    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
    int worldAxisX = SensorManager.AXIS_X;
    int worldAxisZ = SensorManager.AXIS_Z;
    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
    float pitch = orientation[1] * FROM_RADS_TO_DEGS;
    float roll = orientation[2] * FROM_RADS_TO_DEGS;
    ((TextView)findViewById(R.id.pitch)).setText("Pitch: "+pitch);
    ((TextView)findViewById(R.id.roll)).setText("Roll: "+(roll-90));
}

}

How can i fix it? Or please redirect me to a tutorial on the subject.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
j.doe
  • 57
  • 1
  • 3

1 Answers1

0

Add INTERNET permission in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>

And do not access in UI thread,Android forbidden that.You must access internet in other thread.

Change your code as following code snippet:

b.setOnClickListener(new View.OnClickListener() {
    public  void onClick(View v){
       new Thread(){
          @Override
          public void run() {
             try{

                Socket s = new Socket("10.0.0.7", 5555);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                String outMsg = "";
                outMsg = "This is Client";
                out.write(outMsg);
                out.flush();
                out.close();
                s.close();
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       err.setText("sending");
                   }
                });
             }catch(Exception ex){
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       err.setText("error"+ex.toString());
                   }
                });

             }               
        }
    }.start();

}

});

aolphn
  • 2,950
  • 2
  • 21
  • 30