-1

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)
Al-Alamin
  • 1,272
  • 2
  • 12
  • 29
  • Have you tried the answer in https://stackoverflow.com/questions/8965155/cannot-assign-requested-address-jvm-bind ? – Jotunacorn Oct 16 '17 at 06:59
  • Which of these two sends throws the exception? And where are you trying to send the datagram? and can the interface the sending socket is bound to really send to that destination? – user207421 Oct 16 '17 at 07:29
  • Yes. I have seen this and this does not solve my problem – Al-Alamin Oct 16 '17 at 07:30
  • @alalamin I'm not surprised. Half the answers there are incorrect or irrelevant. It's strange that you're getting a bind exception when sending, instead of when binding/creating. Are you sure this is the real code? – user207421 Oct 16 '17 at 07:31
  • Note that there are errors in the Javadoc for the constructors of `DatagramSocket` that take an `InetAddress` or `SocketAddress` argument. The 'wildcard' address is not 'an address chosen by the kernel'. It is INADDR_ANY, which specifically means *any* IP address, out of all the present and future IP addresses the host owns as local addresses. – user207421 Oct 16 '17 at 07:33
  • @ EJP 1. Yes this is a real code. 2. I am getting the exception while sending the packet not while i am in the datagram constructor. datagramSocketWifi.send(sendPacket); datagramSocketet.send(sendPacket); 3. I have given valid ip adress i even folled instruction from here http://docs.oracle.com/javase/tutorial/networking/nifs/definition.html But none of these worked for me – Al-Alamin Oct 16 '17 at 07:50
  • This isn't the real code. You have both `datagramSocketEt` and `datagramSocketet`. They aren't the same. Do you have a third datagram socket ? – user207421 Oct 16 '17 at 07:54

1 Answers1

-1

My server was running on localhost ie 127.0.0.1 when i moved my server to an external host ie IP then this same code worked. I could sent packets using ehternet or wife and could see the log in wireshark.

Al-Alamin
  • 1,272
  • 2
  • 12
  • 29
  • Your server? What server? Your server was 'running on localhost i.e. 127.0.0.1' how? You didn't say anything about servers in your question. The only code you exhibited was *not* 'running on 127.0.0.1'. – user207421 Oct 16 '17 at 09:19
  • EJP sorry my mistake. I did not think it would matter if the server was running on localhost or in a external server – Al-Alamin Oct 16 '17 at 09:22
  • I am asking you what that means, and you haven't answered. Your answer and your question are both useless here until you clarify. – user207421 Oct 16 '17 at 09:22
  • DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, serverPort); Here IPAddress was server IP which was running on localhost – Al-Alamin Oct 16 '17 at 09:27
  • 'Running on localhost' is either meaningless or wouldn't cause this problem. Do you mean the server's `DatagramSocket` was *bound* to 127.0.0.1? As a matter of fact *all* the `DatagramSockets` in this question and elsewhere in your code should be bound to the wildcard address, which manifests itself in the API as an `InetAddress` of `null`, or omitted. – user207421 Oct 16 '17 at 09:30