Bryan
Bryan

Reputation: 95

How to send datagram packets with Java in android

I wrote a function to test sending data

public void send() {

        Runnable myRunnable = new Runnable()
        {

            @Override
            public void run(){
                while(true) {
                    try {
                        byte[] buffer = new byte[1024];
                        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("111.11.111.111"), 65535);
                        DatagramSocket socket = new DatagramSocket();
                        socket.send(packet);
                    } catch (Exception e) {
                    }
                }
            }
        };
        Thread myThread = new Thread(myRunnable);
        myThread.start();
    }

which works on my computer but doesn't seem to do anything on my phone.

I tried calling the function with a button

public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.button:
                test();

                break;

        }
    }

but nothing seems to happen.

Can someone help?

Upvotes: 0

Views: 671

Answers (1)

Bryan
Bryan

Reputation: 95

NVM I found out my problem. Just had to add the android permission for Internet

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

Upvotes: 1

Related Questions