I am trying to send a ByteBuffer array over a socket on the port 25565 on the address "localhost". But for some reason, Java is throwing a connection reset exception when doing input.read(). Could someone please tell me whats going on?
Sender:
private static Socket socket;
public static void main(String[] args) throws IOException {
socket = new Socket("localhost", 25565);
String Password = "1234";
ByteBuffer Buffer = ByteBuffer.allocate(1 + Password.getBytes().length);
Buffer.put((byte) 0x00);
Buffer.putShort((short) Password.getBytes().length);
Buffer.put(Password.getBytes());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.write(Buffer.array());
}
public static void sendBytes(byte[] myByteArray) throws IOException {
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.write("LOL".getBytes());
output.flush();
}
Receiver:
public static void main(String[] args) {
try {
ServerSocket ServerSocket = new ServerSocket(25565);
System.out.println("Waiting for connection...");
Socket socket = ServerSocket.accept();
DataInputStream Input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
System.out.println(Input.read());
ServerSocket.close();
socket.close();
} catch (Exception e) {
if(e instanceof SocketTimeoutException) {
System.out.println("THE SOCKET TIMED OUT!");
}
else {
e.printStackTrace();
}
}
}
Stack trace:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at net.networking.Receiver.main(Receiver.java:17)
NOTE: Yes, I do know that just using input.read() will not get the whole ByteBuffer array I'm trying to send. But right now I just want to read the first byte and print it out to the console.