当前位置: 首页 > 知识库问答 >
问题:

Android:套接字失败:EACCES(拒绝权限)[重复]

西门京
2023-03-14
I/System.out: java.net.SocketException: socket failed: EACCES (Permission denied)
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="16" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
public class MainActivity extends Activity {

private static final int PORT= 8888;
private static final int TIMEOUT_MS = 500;



@Override
protected void onCreate(Bundle savedInstanceState) {
    final TextView tView;
    Button buttonRed, buttonGreen, buttonBlue, buttonWhite;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tView = (TextView) findViewById(R.id.showroom_welcome);

    buttonRed = (Button) findViewById(R.id.buttonred);
    buttonRed.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            /*send message xyz*/
            try {
                String message = "\"xyz\"";
                DatagramSocket socket = new DatagramSocket(PORT);
                socket.setBroadcast(true);
                socket.setSoTimeout(TIMEOUT_MS);
                DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), getBroadcastAddress(), PORT); //InetAddress.getByName("192.168.100.255"), PORT );
                socket.send(packet);
            }catch(UnsupportedEncodingException uee){
                tView.setText(uee.getMessage());
                System.out.println(uee.getMessage());

            }catch (SocketException se){
                tView.setText(se.getMessage() );
                System.out.println(se+"\n");

            }catch (IOException ioe){
                tView.setText(ioe.getMessage());
                System.out.println(ioe + "\n");
            }
        }
    });
}

共有1个答案

东明德
2023-03-14

您只是设置网络状态的权限。只有当您想要了解您所在的网络或想以编程方式更改网络时,才需要该状态。

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

是否错误将其更改为:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

您还需要Internet权限:

<uses-permission android:name="android.permission.INTERNET"/>
 类似资料: