我的筆記本電腦與以太網電纜和wifi連接 我的以太網IP:192.168.18.32 我的無線IP:192.168.18.167 現在我想通過發送一個UDP包java使用wifi網絡接口。 我看了,如果我要綁定把對應的IP,然後在相應的接口將被用來發送,即UDP數據包,如果我綁定到IP 192.168.18.167然後UDP數據包將使用WiFi接口Java的UDP不能綁定到本地IP地址(以太網)
我的代碼發送:
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);
但每當這種發送方法稱爲
datagramSocketWifi.send(sendPacket);
datagramSocketet.send(sendPacket);
它顯示了以下錯誤:
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)
您是否嘗試過 的答案https://stackoverflow.com/questions/8965155/cannot-assign-requested-address-jvm-bind? – Jotunacorn
這兩個發送哪個引發異常?你想在哪裏發送數據報?並且發送套接字的接口是否可以真正發送到該目的地? – EJP
是的。我已經看到了這個,這並沒有解決我的問題 –