My laptop is connected with ethernet cable and wifi my ethernet ip: 192.168.18.32 my wifi ip: 192.168.18.167 Now I would like to send a udp packet through java using wifi network interface. I read that if i bind to the correspoding ip then the corresponding interface will be used to send udp packet ie if i bind to ip 192.168.18.167 then udp packets will sent using wifi interface
my code is:
final String wiFiCardAddressName = "192.168.18.32";
final String ethernetAddressName = "192.168.18.167";
final InetAddress wiFiCardAddress = InetAddress.getByName(wiFiCardAddressName);
final InetAddress ethernetAddress = InetAddress.getByName(ethernetAddressName);
DatagramSocket datagramSocketWifi = new DatagramSocket(10000, wiFiCardAddress);
System.out.println(ethernetAddress);
DatagramSocket datagramSocketEt = new DatagramSocket(6666, ethernetAddress);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, serverPort);
but whenever this send method is called
datagramSocketWifi.send(sendPacket);
datagramSocketet.send(sendPacket);
It shows the following error:
Exception in thread "main" java.net.BindException: Cannot assign requested address: Datagram send failed
at java.net.DualStackPlainDatagramSocketImpl.socketSend(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.send(Unknown Source)
at java.net.DatagramSocket.send(Unknown Source)
at socket_test.Client_UDP_Bind.main(Client_UDP_Bind.java:50)