sockets - Java Soket cannot send multicast packet on Raspberry Pi -
i try run code in raspberry pi. socket can receive data multicast group, however, shows following error when tries send data:
pi@raspberrypi:~ $ java protocol java.net.multicastsocket@647e05 java.io.ioexception: cannot assign requested address @ java.net.plaindatagramsocketimpl.send(native method) @ java.net.datagramsocket.send(datagramsocket.java:693) @ protocol.getsensordata(protocol.java:201) @ protocol.main(protocol.java:305)
here code:
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 { // 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; } }
your java may picking ip v4 instead of ip v6 (like multicast address seems point to). try starting
-djava.net.preferipv6addresses=true
usually happens java preferring v6 on v4, might have special case here. alternatively, you're using invalid multicast address (looking @ https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#ipv6-scope doesn't appear addresses starting ff12
valid ipv6 multicast addresses).
Comments
Post a Comment