Ciao a tutti.
Ho realizzato una componente server che sta in ascolto su un MulticastSocket e una componente client che invia un multicast usando DatagramSocket.
Entrambe le componenti sono in un'applicazione web deployata in tomcat, la parte client viene lanciata mediante una rest api
In locale funziona ma ho alcune domande.
1) il MulticastSocket viene creato ed eseguito in un thread ma quando stoppo l'applicazione ho degli errori nella chiusura di quel thread, il metodo receive() di MulticastSocket è bloccante, come va chiuso il tutto in maniera pulita?
2) Lo scambio di messaggi funziona ma al momento ho provato il tutto in locale; il client si connette utilizzando l'ip di un'interfacca di rete della macchina su cui gira l'applicazione e la porta del tomcat; in un caso reale posso farlo connettere ad un qualunque fqdn?
3) Che modifiche devo fare per gestire una connessione sicura su cui sono stati impostati dei certificati nella cacerts della jvm?
Codice del server:
codice:
private static final String MULTICAST_ADDRESS_IPV4 = "239.255.255.250";
private static final int PORT = 5555;
private static final int DATAGRAM_PACKET_LENGHT = 4096;
private void startSocket() {
try {
MulticastSocket socket = new MulticastSocket(ONVIF_PORT);
InetAddress discoveryIpAddress = InetAddress.getByName(MULTICAST_ADDRESS_IPV4);
socket.joinGroup(discoveryIpAddress);
DatagramPacket packet = new DatagramPacket(buf, DATAGRAM_PACKET_LENGHT);
if (packet != null) {
String received = new String(packet.getData(), 0, packet.getLength());
logger.debug("startSocket-> received: " + received);
String rsp = // preparo la risposta
byte[] msgRsp = rsp.getBytes();
DatagramPacket packetAcknoledge = new DatagramPacket(msgRsp, msgRsp.length, packet.getAddress(), packet.getPort());
socket.send(packetAcknoledge);
}
}
catch (SocketException e) {
logger.error("startSocket - SocketException: " + e.toString());
}
catch (IOException e) {
logger.error("startSocket - IOException : " + e.toString());
}
}
Codice del client
codice:
private static final String MULTICAST_ADDRESS_IPV4 = "239.255.255.250";
private static final int PORT = 5555;
public static int DISCOVERY_TIMEOUT = 10000;
public String testMulticastSocket(int port, String address) {
String packet = // pacchetto da inviare
byte[] data = packet.getData();
InetAddress iaddress = InetAddress.getByName(address);
DatagramSocket client = new DatagramSocket(port, iaddress);
client.setBroadcast(true); client.setSoTimeout(DISCOVERY_TIMEOUT);
client.send(new DatagramPacket(data, data.length, InetAddress.getByName(MULTICAST_ADDRESS_IPV4), PORT));
boolean socketTimeout = false;
DatagramPacket receiverPacket = new DatagramPacket(new byte[4096], 4096);
while (!socketTimeout) {
try {
client.receive(receiverPacket);
if (receiverPacket != null) {
String response = new String(receiverPacket.getData(), 0, receiverPacket.getLength());
logger.debug("probeSync -> response: " + response);
}
}
catch (SocketTimeoutException e) {
socketTimeout = true;
}
}
client.close();
}
Grazie a chi risponderà e buone feste