简体   繁体   English

为什么握手后我的套接字关闭了,我该如何解决这个问题?

[英]Why did my socket close after the handshake and how can I fix this?

I am trying to connect a website to my java plugin (in minecraft) and let them communicate and play audio.我正在尝试将一个网站连接到我的 java 插件(在我的世界中)并让他们交流和播放音频。 When I try to send a connected message.当我尝试发送已连接的消息时。 Server starts when the plugin is enabled.服务器在插件启用时启动。

Code: Executed when site connects to the plugin.代码:当站点连接到插件时执行。

Socket client = AudioServer.getServer().accept();
            client.setKeepAlive(true);
            client.setTcpNoDelay(true);
            
            BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter output = new PrintWriter(client.getOutputStream());

            String line;
            String key = "";
            while (true) {
                line = input.readLine();
                if(line.startsWith("Sec-WebSocket-Key: ")) {
                    key = line.split(" ")[1];
                }
                if (line == null || line.isEmpty())
                    break;
            }
            output.println("HTTP/1.1 101 Switching Protocols");
            output.println("Upgrade: websocket");
            output.println("Connection: Upgrade");
            output.println("Sec-WebSocket-Accept: " + encode(key));
            output.println();
            output.flush();
            input.close();
            
            AudioClient ac = new AudioClient(client);

    public AudioClient(Socket client) {
    try {           
        this.Client = client;
        this.conn = false;
        
        client.setKeepAlive(true);
        client.setTcpNoDelay(true);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    
    Thread t = new Thread(new AudioReceiver(this));
    t.start();
    
    this.sendData("connected");
}

Console:安慰:

[10:06:34] [Thread-315/WARN]: java.net.SocketException: Socket is closed
[10:06:34] [Thread-315/WARN]:   at java.net.Socket.setKeepAlive(Socket.java:1309)
[10:06:34] [Thread-315/WARN]:   at ev.Audio.AudioClient.<init>(AudioClient.java:23)
[10:06:34] [Thread-315/WARN]:   at ev.Audio.AudioConnection.run(AudioConnection.java:42)
[10:06:34] [Thread-315/WARN]:   at java.lang.Thread.run(Thread.java:748)
[10:06:34] [Thread-315/WARN]: java.net.SocketException: Socket is closed
[10:06:34] [Thread-315/WARN]:   at java.net.Socket.getOutputStream(Socket.java:943)
[10:06:34] [Thread-315/WARN]:   at ev.Audio.AudioClient.sendData(AudioClient.java:42)
[10:06:34] [Thread-315/WARN]:   at ev.Audio.AudioClient.<init>(AudioClient.java:33)
[10:06:34] [Thread-315/WARN]:   at ev.Audio.AudioConnection.run(AudioConnection.java:42)
[10:06:34] [Thread-315/WARN]:   at java.lang.Thread.run(Thread.java:748)

When closing the InputReader using input.close();使用input.close(); , you also close the underlying Socket connection. ,你也关闭了底层的 Socket 连接。

As you can see in this question, you cannot close the BufferedReader without closing the Socket.正如您在这个问题中看到的那样,您不能在不关闭 Socket 的情况下关闭 BufferedReader。 Your only options would be to either pass the BufferedReader to your AudioClient , or to use a DataInputStream / BufferedInputStream to read the data from.您唯一的选择是将 BufferedReader 传递给您的AudioClient ,或使用DataInputStream / BufferedInputStream从中读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM