0

I want to send an UDP packet form my mobile device to my computer, but when I try to initiate a DatagramSocket or use the InetAddress.getByName() method the compiler shows the folowing errors:

DatagramSocket: "Unhandeld Exception: java.net.UnknowHostException"

InetAddress: "Unhandeld Exception: java.net.SocketException"

I also added a few user-permission tags in the AndroidMafinest.xml file, but without result.

PacketService.java

package mw.mobilepccommunication;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class PacketService {

    private String mIP;
    private int mPort;

    public PacketService (String aIP, int aPort)
    {
        mIP = aIP;
        mPort = aPort;
    }

    public void lockHandler ()
    {
        String lMessage = "TEST";
        InetAddress lLocal = InetAddress.getByName(mIP); //<--UnknownHostException
        int lMessageLength = lMessage.length();
        byte [] lBuffer = lMessage.getBytes();

        DatagramSocket lSocket = new DatagramSocket(mPort); // <--SocketException 

        DatagramPacket lPacket = new DatagramPacket(lBuffer, lMessageLength, lLocal, mPort);
    }

}

AndroidManifest.xml

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

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

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

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

</manifest>
unskull
  • 3
  • 4
  • What it says: Your device does not know how to find the ip for the name you provide. Are you putting your pc's network name there or some ip address? The windows name would require special resolver magic like http://stackoverflow.com/questions/12767893/ip-address-to-netbios-fqdn-name-in-java-android – zapl Oct 17 '15 at 20:16
  • I used an ip, in this case "192.168.1.1". – unskull Oct 17 '15 at 20:41
  • Are your phone and your pc on the same network? I think it actually checks if it can reach that ip. If it can't it would tell you that the host is unknown. – zapl Oct 17 '15 at 20:54
  • It is in the same network. I pinged the android device from my pc. – unskull Oct 17 '15 at 21:03
  • 'Sending an UDP message over WiFi causes UnkownHostException and SocketException'. No. You have two compiler warnings. Your subject is completely wrong as you are not trying to send something at runtime but trying to compile some code at ... compile time. Just add the two catch blocks. The IDE will even do it for you. Your app will not even run. To the other posters you reacted as if your app runs. – greenapps Oct 18 '15 at 04:00

0 Answers0