0

I just started learning Android and I am trying to successfully send a string from UDP client

I am using PacketSender as the server, but I haven't gotten so far, because my permission is denied.

Here is the code:

public class UDPStringSender extends AppCompatActivity {


Intent intent;
String ipAddress, port;
int portNumber;

boolean shouldLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_udp_string_sender);

    intent = getIntent();
    ipAddress = intent.getStringExtra("ip");
    port = intent.getStringExtra("port");
    portNumber = Integer.parseInt(port);

    Log.d("IP number is", ipAddress);
    Log.d("Port number is", port);

    shouldLog = true;
}


public void sendString(View view) {
    EditText stringField = (EditText)findViewById(R.id.sendableString);
    String sendableString = stringField.getText().toString();

    DatagramSocket _datagramSocket = null;
    try {
        _datagramSocket = new DatagramSocket();

        InetAddress serverIP = InetAddress.getByName(ipAddress);
        if(shouldLog)
            Log.d("InetAddress is", serverIP.toString());
        DatagramPacket _datagramPacket = new DatagramPacket(sendableString.getBytes(), sendableString.length(), serverIP, portNumber);
        if(shouldLog)
            Log.d("Packet to be send", _datagramPacket.toString());
        _datagramSocket.send(_datagramPacket);

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(_datagramSocket != null) {
            _datagramSocket.close();
            if(shouldLog)
                Log.d("is datagramSockedClosed", String.valueOf(_datagramSocket.isClosed()));
        }
    }
}

}

My android manifest:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <activity android:name=".Interface">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".UDPStringSender">
    </activity>
</application>

As well as my logcat:

11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:  java.net.SocketException: socket failed: EACCES (Permission denied)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:619)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:93)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.net.DatagramSocket.createSocket(DatagramSocket.java:157)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.net.DatagramSocket.<init>(DatagramSocket.java:65)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at co.barakastudio.layouttest.UDPStringSender.sendString(UDPStringSender.java:48)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.view.View.performClick(View.java:5697)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.widget.TextView.performClick(TextView.java:10826)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.os.Looper.loop(Looper.java:158)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7225)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at libcore.io.Posix.socket(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:604)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err:     ... 17 more

I really appreciate your time, as it will save tons of mine :) Cheers,

1

2 Answers 2

1

Quoting myself:

The <uses-permission> elements should be inside the <manifest> element, but outside the <application> element. In other words, this is fine:

<?xml version="1.0" encoding="utf-8"?>
<manifest
  package="com.commonsware.android.something.something"
  xmlns:android="http://schemas.android.com/apk/res/android">

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

  <application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- cool stuff goes here -->

  </application>

</manifest>

but this is not:

<?xml version="1.0" encoding="utf-8"?>
<manifest
  package="com.commonsware.android.something.something"
  xmlns:android="http://schemas.android.com/apk/res/android">

  <application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

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

    <!-- cool stuff goes here -->

  </application>

</manifest>

While Android Studio will report this if you manually analyze your manifest (Analyze > Inspect Code... from the main menu), it will not automatically show a warning just by having the elements in the wrong spot.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know you cannot use sockets in the main thread... Android just wont allow that.

I never tried DatagramSocket, just the usual Socket but I think it doesn't make a difference.

You should create a new thread and send the String there.

Quick example (not best practise but quite short ;):

Thread thread = new Thread(new Runnable() {
     public void run() {
          // send the String here
     }
});  
thread.start();

This will send the String in the background and as soon as the method run() is terminated, the thread will be destroyed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.