3

I have this message in my console:

[15:06:59] [Thread-65394/WARN]: java.net.SocketException: Connection reset
[15:06:59] [Thread-65394/WARN]:     at java.net.SocketInputStream.read(SocketInputStream.java:209)
[15:06:59] [Thread-65394/WARN]:     at java.net.SocketInputStream.read(SocketInputStream.java:141)
[15:06:59] [Thread-65394/WARN]:     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
[15:06:59] [Thread-65394/WARN]:     at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
[15:06:59] [Thread-65394/WARN]:     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
[15:06:59] [Thread-65394/WARN]:     at java.io.InputStreamReader.read(InputStreamReader.java:184)
[15:06:59] [Thread-65394/WARN]:     at java.io.BufferedReader.fill(BufferedReader.java:161)
[15:06:59] [Thread-65394/WARN]:     at java.io.BufferedReader.read(BufferedReader.java:182)
[15:06:59] [Thread-65394/WARN]:     at *hidden*.Reception.run(Reception.java:38)
[15:06:59] [Thread-65394/WARN]:     at java.lang.Thread.run(Thread.java:745)

I don't know why I do have this error, here is my code (it's a bukkit plugin):

Main.java:

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
    public static Main instance = null;

    public void onEnable() {
        instance = this;
        final Thread t = new Thread(new Init());
        t.start();
    }
    public void onDisable() {
    }
}

Init.java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Init implements Runnable {
    public Main main;

    public Init(Main instance) {
        main = instance;
    }
    public Init(){
    }
    public void run() {
        while (true){
            try{
                Socket _socket = new Socket("localhost", 8004);
                BufferedReader in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
                PrintStream out = new PrintStream(_socket.getOutputStream());
                out.println("helloworld\0");//ask server for the password to encrypt, \0 is the line separator
                final Thread t1 = new Thread(new Reception(in, out, _socket));
                t1.start();
                while (_socket.isClosed())break;
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for ");
                System.exit(1);
            }
        }
    }
}

Reception.java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

public class Reception implements Runnable {

    private BufferedReader in;
    private PrintStream out;
    private Socket _socket;

    public Reception(BufferedReader in, PrintStream out, Socket _socket){
        this.in = in;
        this.out = out;
        this._socket = _socket;
    }

    public String remlastchar(String str) {} //remove last char

    public String encryptpass(String str) {
        str = somesecretcode...;
        return str;
    }

    private StringBuilder sb = new StringBuilder();
    private int ch;
    private String line = "";

    public void run() {
        try {
            while(true){
                if(_socket.isClosed())break;
                if((ch = in.read()) >= 0) { //LINE 38, HERE IS THE ERROR !
                    sb.append((char) ch);
                    if(ch == '\0'){ //line separator is \0 instead of \n
                        line=sb.toString();
                        sb.delete(0, sb.length());
                        if(line.length() == 15){ //the "password" contains 14 chars.
                            in.close();
                            out.println("login " + encryptpass(remlastchar(line)) + "\0"+someinstructions+"\0"); //encrypt the "password" and send it to the server
                            out.close();
                            try {
                                _socket.close();
                                break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Actually, I want my bukkit plugin to do this in a loop:

  1. Connect to a TCP server
  2. Send a message to the TCP server
  3. Wait for the answer (14chars)
  4. Encrypt the answer and send it back with some more instructions (~1Ko)
  5. Close the socket once data sent

Does it mean that my data hasn't been sent or whatever?

Jules420
  • 71
  • 7
  • tl;dr That Exception is a sign for the other side to "hang up" on you. Can you see logs of the server to see if there are any exceptions at the same time? – Fildor Jul 15 '16 at 14:05
  • No I can't, but I first made this using Ruby 2.0.0, it worked well and I translated it in Java. I know that (some of?) the instructions I send are being read by the server because when I'm on the minecraft server, what I want to do is done (but I don't know if everything is done) – Jules420 Jul 15 '16 at 14:16
  • `socket.isClosed()` only means whether you have called `socket.close()`. So you may want another way to check the connection availability? – Cnly Jul 15 '16 at 15:58
  • I forgot to say, the server only accepts one connection per IP, maybe I'm trying to connect while my past socket isn't closed for the server? Cnly, the server never closes the connection alone I think – Jules420 Jul 15 '16 at 16:05
  • @Jules420 then you should try to close it first. Close the socket if `in.read()` reaches the end of the stream. – Cnly Jul 15 '16 at 23:39

0 Answers0