Java Socket NIO Reading Issue

(code in comments) I have a server and a client. I am trying to transmit a long from the server to the client (or reverse, neither works). I can tell it is transmitting, but the reciever gets hung up on the socket.read(buf) line in readLong. Please help. I just started NIO this morning and have no clue if I'm doing it right.
3 Replies
JavaBot
JavaBot8mo ago
This post has been reserved for your question.
Hey @The Typhothanian! 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.
The Typhothanian
The TyphothanianOP8mo ago
package net.typho.utils.nio;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class ChannelHandler implements Closeable {
public final SocketChannel socket;

public ChannelHandler(SocketChannel socket) {
this.socket = socket;
}

@Override
public String toString() {
try {
return socket.getRemoteAddress().toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void close() throws IOException {
socket.close();
}

public void write(byte... data) throws IOException {
ByteBuffer buf = ByteBuffer.wrap(data).flip();

while (buf.hasRemaining()) {
socket.write(buf);
}
}

public void write(long... data) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(data.length * Long.BYTES);

for (long l : data) {
buf.putLong(l);
}

buf.flip();

while (buf.hasRemaining()) {
System.out.println(socket.write(buf));
}
}
package net.typho.utils.nio;

import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class ChannelHandler implements Closeable {
public final SocketChannel socket;

public ChannelHandler(SocketChannel socket) {
this.socket = socket;
}

@Override
public String toString() {
try {
return socket.getRemoteAddress().toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void close() throws IOException {
socket.close();
}

public void write(byte... data) throws IOException {
ByteBuffer buf = ByteBuffer.wrap(data).flip();

while (buf.hasRemaining()) {
socket.write(buf);
}
}

public void write(long... data) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(data.length * Long.BYTES);

for (long l : data) {
buf.putLong(l);
}

buf.flip();

while (buf.hasRemaining()) {
System.out.println(socket.write(buf));
}
}
public List<Byte> read() throws IOException {
List<Byte> bytes = new ArrayList<>();
ByteBuffer buf = ByteBuffer.allocate(16);

while (socket.read(buf) > 0) {
buf.flip();

while (buf.hasRemaining()) {
bytes.add(buf.get());
}

buf.clear();
}

return bytes;
}

public List<Long> readLong() throws IOException {
List<Long> longs = new ArrayList<>();
ByteBuffer buf = ByteBuffer.allocate(Long.BYTES);

while (socket.read(buf) > 0) {
buf.flip();

longs.add(((buf.get() & 0xFFL) << 56) |
((buf.get() & 0xFFL) << 48) |
((buf.get() & 0xFFL) << 40) |
((buf.get() & 0xFFL) << 32) |
((buf.get() & 0xFFL) << 24) |
((buf.get() & 0xFFL) << 16) |
((buf.get() & 0xFFL) << 8) |
((buf.get()) & 0xFFL));

buf.clear();
}

return longs;
}
}
public List<Byte> read() throws IOException {
List<Byte> bytes = new ArrayList<>();
ByteBuffer buf = ByteBuffer.allocate(16);

while (socket.read(buf) > 0) {
buf.flip();

while (buf.hasRemaining()) {
bytes.add(buf.get());
}

buf.clear();
}

return bytes;
}

public List<Long> readLong() throws IOException {
List<Long> longs = new ArrayList<>();
ByteBuffer buf = ByteBuffer.allocate(Long.BYTES);

while (socket.read(buf) > 0) {
buf.flip();

longs.add(((buf.get() & 0xFFL) << 56) |
((buf.get() & 0xFFL) << 48) |
((buf.get() & 0xFFL) << 40) |
((buf.get() & 0xFFL) << 32) |
((buf.get() & 0xFFL) << 24) |
((buf.get() & 0xFFL) << 16) |
((buf.get() & 0xFFL) << 8) |
((buf.get()) & 0xFFL));

buf.clear();
}

return longs;
}
}
Tried simply sending a byte, no luck
JavaBot
JavaBot8mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server