Skip to content

Commit a3eb876

Browse files
committed
Further adjustments to socket client handling
1 parent 1a46f98 commit a3eb876

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

AdvancedCore/src/com/bencodez/advancedcore/bungeeapi/sockets/ClientHandler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package com.bencodez.advancedcore.bungeeapi.sockets;
33

44
import java.io.DataOutputStream;
5-
import java.io.IOException;
65
import java.net.Socket;
76

87
import com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler;
@@ -43,23 +42,26 @@ public void sendMessage(boolean debug, String... msgs) {
4342
if (debug) {
4443
System.out.println("Socket Sending: " + ArrayUtils.makeStringList(ArrayUtils.convert(msgs)));
4544
}
46-
connect();
45+
4746
String msg = msgs[0];
4847
for (int i = 1; i < msgs.length; i++) {
4948
msg += "%line%";
5049
msg += msgs[i];
5150
}
52-
if (clientSocket == null) {
51+
52+
connect();
53+
if (clientSocket == null || clientSocket.isClosed()) {
5354
System.out.println("Failed to connect to " + host + ":" + port + " to send message: " + msg);
5455
return;
5556
}
5657
String encrypted = encryptionHandler.encrypt(msg);
5758
try (DataOutputStream ds = new DataOutputStream(clientSocket.getOutputStream())) {
5859
ds.writeUTF(encrypted);
59-
} catch (IOException e1) {
60+
} catch (Exception e1) {
6061
e1.printStackTrace();
62+
} finally {
63+
stopConnection();
6164
}
62-
stopConnection();
6365
}
6466

6567
public void sendMessage(String... msgs) {
@@ -70,6 +72,7 @@ public void stopConnection() {
7072
try {
7173
if (clientSocket != null) {
7274
clientSocket.close();
75+
clientSocket = null;
7376
}
7477
} catch (Exception e) {
7578
e.printStackTrace();

0 commit comments

Comments
 (0)