简体   繁体   中英

W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied) in android

Tying push the data from my application to local server.

 public class MainActivity extends AppCompatActivity {
 Socket socket;
 private static final String SERVER_IP = "127.0.0.1";//server ip

 private static final int SERVERPORT = 8000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
}
private class ClientThread implements Runnable {
    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

            socket = new Socket(serverAddr, SERVERPORT);

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

In the manifest file I have registered below permissions

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

getting below error:

  W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
  I/zygote: NativeAllocBackground concurrent copying GC freed 2066(637KB) AllocSpace 
  objects, 0(0B) LOS objects, 73% free, 547KB/2MB, paused 13.469ms total 130.017ms
  W/System.err:     at java.net.Socket.createImpl(Socket.java:487)
  W/System.err:     at java.net.Socket.<init>(Socket.java:441)
  W/System.err:     at java.net.Socket.<init>(Socket.java:248)
  W/System.err:     at com.abc.MainActivity$ClientThread.run(MainActivity.java:85)

Can you please help me to resolve this issue.

There's a typo in the manifest: PROmission instead of PERmission!

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

should say

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

Your app is running on an emulator. If your app uses localhost address 127.0.0.1 it tries to connect to a server also running on that emulator.

If your server is running on the same pc your emulator is running on your app should use ip address 10.0.2.2 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM