Help with sending a packet

I am making a socket server and trying to send a Server -> Client packet but I am getting a method ChiropteraServer#broadcast called with (Packet[id=4, data={0=abc, 1=asd}] (Packet)) threw a IllegalBlockingModeException: null I tried a few stuff but I couldn't get it to work, one of the attempts is the one on github The sendPacket method: https://github.com/erenkarakal/Chiroptera/blob/master/src/main/java/me/eren/chiroptera/ChiropteraServer.java#L128
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

client.configureBlocking(true);
oos.writeObject(packet);
client.configureBlocking(false);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

client.configureBlocking(true);
oos.writeObject(packet);
client.configureBlocking(false);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
!!! ping me if you respond please !!!
4 Replies
JavaBot
JavaBot9mo ago
public static void sendPacket(String clientIdentifier, Packet packet) {
public static void sendPacket(String clientIdentifier, Packet packet) {
This post has been reserved for your question.
Hey @eren.! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan9mo ago
Have you tried leaving configureBlocking() alone?
eren.
eren.OP9mo ago
you mean dont use it at all? i think i did but ill do it again yeah, same issue
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

oos.writeObject(packet);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

oos.writeObject(packet);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
fixed it with a code i 'borrowed' from an old stackoverflow post
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try {
ByteBuffer buffer = ByteBuffer.wrap(packet.serialize());
while (buffer.hasRemaining()) {
client.write(buffer);
}
} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending data to client. " + e.getMessage());
}
}
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try {
ByteBuffer buffer = ByteBuffer.wrap(packet.serialize());
while (buffer.hasRemaining()) {
client.write(buffer);
}
} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending data to client. " + e.getMessage());
}
}
JavaBot
JavaBot9mo ago
Post Closed
This post has been closed by <@587604022349791274>.
Want results from more Discord servers?
Add your server