Sending DatagramPacket without internet connection - Android

I use the following code to send a DatagramPacket to a given address:

 InetAddress address = InetAddress.getByName(anIPAddress); DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(packetBytes, packetBytes.length, address, port); socket.send(packet); socket.close(); 

It works fine, but how does this code throw no Exception when there is no internet connection available?

I will disable both Wi-Fi and mobile data, and this code still executes without any errors.

Is there any way to make sure the package is really shipped?

(I donโ€™t care if it reached the goal or not, I just wanted to make sure it was sent)

+7
java android udp
source share
2 answers

You can use NetworkInterface.getNetworkInterfaces(); to check which interfaces are available. Then you can check with ni.isUp() .

 Enumeration<NetworkInterface> nets=NetworkInterface.getNetworkInterfaces(); for(int i=0; nets.hasMoreElements(); ++i) { NetworkInterface ni=nets.nextElement(); if (ni.isUp() && !ni.getName().equals("lo")) { //ni is not local break; } } 
+1
source share

Connect the socket to the dataset. Then after several submissions you can get an exception. If the datagram socket is not connected, a routing error occurs that I cannot remember in advance: I will edit it when you have the opportunity to see it.

And yes, I know that UDP is a connectionless protocol. But you can still connect a UDP socket.

0
source share

All Articles