1

I try to send a UDP packet from my mobile to a PC – IP 192.168.1.113, at the PC I use “Packet sender”with UDP server port 55777. But it does not work, I have sent UDP packet from a VBA script in Excel to the Packet sender and it works, I can also send through UDP apps I have downloaded to the mobile, to the “Packet sender”.

I use:
Android Studio 3.5
SDK platform 8.1
Mobile: Samsung S9+

I have added the INTERNET permission to the manifest:

I send the packet from a new thread since the main thread does not allow to send packages, the below code is the first step before I continue, need to make this to work first though.

public class MainActivity extends AppCompatActivity {

    private Handler mainHandler = new Handler();

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

    }

    public void startThread(View view) {

        ExampleRunnable runnable = new ExampleRunnable(10);
        new Thread(runnable).start();

    }


    class ExampleRunnable implements Runnable {

        @Override
        public void run() {

            DatagramSocket ds = null;

            String message = "Hello";
            try{
                ds = new DatagramSocket();
                InetAddress serverAddr = InetAddress.getByName("192.168.1.113");
                DatagramPacket dp;
                dp = new DatagramPacket(message.getBytes(), message.length(), serverAddr, 55777);
                ds.send(dp);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ds != null) {
                    ds.close();
                }
            }

        }
    }
}
Abu Noman
  • 435
  • 3
  • 13
Eric_TS
  • 19
  • 2
  • 2
    There is no internet, It's local connection based. am i right? if there is any error/response from the program, please post the logs here. – Kiran Maniya Sep 02 '19 at 08:07
  • 1
    Add your stack trace to understand the problem. You can follow this code [snippet](https://stackoverflow.com/a/57713029/4544797). – Abu Noman Sep 02 '19 at 10:10

1 Answers1

-1

There is several things to be confirmed before answering the question. You can provide stack trace or any error if there is any. In your case, If there is no programming error, You should check your firewall once. Add new firewall rule for the incoming connection request on port 55777.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81