나는이 코드를 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;
}
}
IPV6 설정을 사용해 보았습니다. 작동하지 않습니다. 이 주소는 정확하며 인턴 서버 주소입니다. Windows 컴퓨터에서 위 코드를 실행할 수 있습니다. 그러나 Pi에서 왜 작동하지 않는지 확실하지 않습니다. 너는 어떤 생각을 가지고 있니? – Sean