Java Soket无法在Raspberry Pi上发送多播数据包

问题描述:

我尝试在Raspberry Pi中运行此代码。插座可以从组播组接收数据,但是,它显示了以下错误,当它试图发送数据:Java Soket无法在Raspberry Pi上发送多播数据包

[email protected]:~ $ java Protocol 
[email protected] 
java.io.IOException: Cannot assign requested address 
    at java.net.PlainDatagramSocketImpl.send(Native Method) 
    at java.net.DatagramSocket.send(DatagramSocket.java:693) 
    at Protocol.getSensorData(Protocol.java:201) 
    at Protocol.main(Protocol.java:305) 

下面是代码:

import java.net.*; 
import java.util.*; 

public class Protocol { 

    private MulticastSocket socket = null; 
    private MulticastSocket socket_switchPanel = null; 
    String MULTICAST_ADDRESS = ""; 
    int port = -1; 

    public Protocol(String NIC, boolean isByIP, String multcastAddress, int port) { 
     this.MULTICAST_ADDRESS = multcastAddress; 
     this.port = port; 
     try { 
      InetAddress dstAddr = InetAddress.getByName(MULTICAST_ADDRESS); 
      socket = new MulticastSocket(port); 
      InetSocketAddress socketAddress = new InetSocketAddress(MULTICAST_ADDRESS, port); 

      NetworkInterface ni = NetworkInterface.getByName(NIC); 
      socket.setReuseAddress(true); 
      socket.joinGroup(socketAddress, ni); 
      System.out.println(ni); 

     } catch (Exception e) { 
      System.out.println(e); 
      e.printStackTrace(); 
      if (socket != null) { 
       socket.close(); 
      } 
     } 
    } 

    public void close_socket() { 
     if (socket != null) { 
      socket.close(); 
     } 
    } 

    public Integer getSensorData() { 

     byte[] msg = new byte[]{ 
       (byte) 0xAB, 0x04, (byte) 0x82, (byte) 0xCD, 0x00, (byte) 0x01}; 
     try { 
      // get the IP address 
      InetAddress dstAddr = InetAddress.getByName(MULTICAST_ADDRESS); 

      final DatagramPacket out = new DatagramPacket(msg, msg.length, dstAddr, port); 
      socket.send(out); 
      // receive data until timeout or stop prematurely on users' request 
      try { 
       // process multi cast response(s) 
       final byte[] inputBuffer = new byte[30]; 
       final DatagramPacket in = new DatagramPacket(inputBuffer, inputBuffer.length); 
       socket.setSoTimeout(500); 
       socket.receive(in); 
       byte[] data = in.getData(); 
       return 1; 

      } catch (Exception ste) { 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return 1; 
    } 


    public static void main(String[] args) { 
//  tring NIC, boolean isByIP, String multcastAddress, int port 
     Protocol dc = new Protocol("wlan0", 
       false, 
       "ff12:00:00:00:4479:00:00:00", 
       50000); 
     int ab = dc.getSensorData(); 
     System.out.println(ab); 
     return; 
    } 
} 

我找到了解决方案。你必须禁用其他接口。例如,如果您使用WIFI发送多播消息。您可以使用 ifconfig eth0 down来禁用eth0。请注意,PI重新启动后,eth0将再次启用。

您的Java可捡IP V4而不是IP V6(就像你的多播地址似乎指向)。试着用

-Djava.net.preferIPv6Addresses =真

这通常发生在Java宁愿在V4 V6启动它,但你可能在这里有一个特殊的情况。或者,您只是使用无效的多播地址(查看https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#ipv6-scope它不会显示以ff12开头的地址对于ipv6多播地址有效)。

+0

我试图使用IPV6设置。这是行不通的。这个地址是正确的,它是我们的实习生服务器地址。我可以在Windows机器上运行上面的代码。但不知道为什么它在Pi中不起作用。你有什么主意吗? – Sean