0

So I made a simple UDP Client for android, and for some reason it doesn't want to work. I've been struggling with this for like 5 hours now, and I can't find the problem. I even looked almost all the tutorials on the internet, to compare the codes, but I had no luck.

String serverString = "192.168.1.109";
int port = 7777;

Log.d("adam", "Debug");

DatagramSocket socket = null ;

String msg = "Hello World!";

try {
    socket = new DatagramSocket() ;

    InetAddress host = InetAddress.getByName(serverString);
    byte [] data = msg.getBytes() ;
    DatagramPacket packet = new DatagramPacket( data, data.length, host, port );
    Log.d("adam", "Debug2");

    socket.send(packet) ;

    Log.d("adam", "Packet sent" );
} catch( Exception e )
{
    Log.d("adam", "Exception");
    Log.e("adam", Log.getStackTraceString(e));
}
finally
{
    if( socket != null ) {
        socket.close();
    }
}

My mainfest:

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

Here is the debug img:

logcat image

As you can see the "Packet sent" text is not in the logcat. So the problem is probably with the send() function. Any idea what could be the problem?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Adam Kadar
  • 15
  • 7
  • 1
    network on main thread is not allowed by default. there is special function to change that behavior. you can disable that strict mode by `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` – Mark Zucchini Dec 29 '15 at 21:02
  • Well, I know now. Just found a thread with networking issue on android. And they had the same solution there :D I tested it and worked fine. (Link: http://stackoverflow.com/questions/16439587/android-os-networkonmainthreadexception-with-android-4-2) Is this the easiest method to solve this issue? Thanks for the help btw. :) Sorry for opening this thread, but i was busy solving this issue all day, and I tought I never gonna find out what's the problem. – Adam Kadar Dec 29 '15 at 21:07
  • 1
    Best to avoid doing any I/O on the Android main (UI) thread for a production application. A blocking socket call will cause poor application responsiveness. Use an AsyncTask or a dedicated thread to implement your client protocol. – selbie Dec 29 '15 at 21:36

1 Answers1

0

Never ever run networking on the main thread. Do not do the work around that Mark suggests.

When you send data you should use an ASyncTAsk or a Thread.

Here is a very simple threaded function:

Thread sendDate = new Thread() {
@Override
public void run() {
    String serverString = "192.168.1.109";
    int port = 7777;

    Log.d("adam", "Debug");

    DatagramSocket socket = null ;

    String msg = "Hello World!";

    try {
        socket = new DatagramSocket() ;

        InetAddress host = InetAddress.getByName(serverString);
        byte [] data = msg.getBytes() ;
        DatagramPacket packet = new DatagramPacket( data, data.length, host, port );
        Log.d("adam", "Debug2");

        socket.send(packet) ;

        Log.d("adam", "Packet sent" );
    } catch( Exception e )
    {
        Log.d("adam", "Exception");
        Log.e("adam", Log.getStackTraceString(e));
    }
    finally
    {
        if( socket != null ) {
            socket.close();
        }
    }
    }
};
sendDate.start(); 

If you want to tell the user if the data was sent then you should use an ASyncTask

apmartin1991
  • 3,064
  • 1
  • 23
  • 44