连接重置(SocketException)

时间:2016-07-15 13:58:51

标签: java bukkit socketexception tcpsocket

我在控制台中收到此消息:

[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)

我不知道为什么我有这个错误,这是我的代码(它是一个bukkit插件):

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();
        }
    }
}

实际上,我希望我的bukkit插件能够循环执行:

  1. 连接到TCP服务器
  2. 向TCP服务器发送消息
  3. 等待答案(14chars)
  4. 加密答案并再发送一些说明(~1Ko)
  5. 发送数据后关闭套接字
  6. 这是否意味着我的数据尚未发送或其他什么?

0 个答案:

没有答案