From 8133ce2e8dff82f8874f46b9890b2df0f4b9a6e8 Mon Sep 17 00:00:00 2001 From: trifel Date: Wed, 13 Feb 2019 15:59:08 +0100 Subject: [PATCH] Fix: Close socket on shutdown --- src/main/java/com/ictreport/ixi/api/Api.java | 4 ++++ src/main/java/com/ictreport/ixi/api/Sender.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ictreport/ixi/api/Api.java b/src/main/java/com/ictreport/ixi/api/Api.java index d13c520..e929aa1 100644 --- a/src/main/java/com/ictreport/ixi/api/Api.java +++ b/src/main/java/com/ictreport/ixi/api/Api.java @@ -36,6 +36,10 @@ public void init() { public void shutDown() { receiver.shutDown(); sender.shutDown(); + try { + this.socket.close(); + } catch (Exception e) { + } } public Receiver getReceiver() { diff --git a/src/main/java/com/ictreport/ixi/api/Sender.java b/src/main/java/com/ictreport/ixi/api/Sender.java index d60a5b1..b0f7df8 100644 --- a/src/main/java/com/ictreport/ixi/api/Sender.java +++ b/src/main/java/com/ictreport/ixi/api/Sender.java @@ -109,7 +109,7 @@ public void send(final Payload payload, String host, final int port) { public synchronized void send(final Payload payload, final InetSocketAddress address) { try { final byte[] messageByteArray = Payload.serialize(payload).getBytes(); - socket.send(new DatagramPacket(messageByteArray, messageByteArray.length, address)); + if (socket != null && !socket.isClosed()) socket.send(new DatagramPacket(messageByteArray, messageByteArray.length, address)); } catch (final IOException | RuntimeException e) { e.printStackTrace(); }