为什么我的套接字在握手后关闭,我该如何解决?
Why did my socket close after the handshake and how can I fix this?
我正在尝试将一个网站连接到我的 java 插件(在 minecraft 中)并让它们进行通信和播放音频。当我尝试发送已连接的消息时。服务器在启用插件时启动。
代码:
当站点连接到插件时执行。
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");
}
控制台:
[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)
使用 input.close();
关闭 InputReader 时,您也关闭了底层 Socket 连接。
正如您在 问题中看到的,您不能在不关闭 Socket 的情况下关闭 BufferedReader。您唯一的选择是将 BufferedReader 传递给您的 AudioClient
,或者使用 DataInputStream
/BufferedInputStream
从中读取数据。
我正在尝试将一个网站连接到我的 java 插件(在 minecraft 中)并让它们进行通信和播放音频。当我尝试发送已连接的消息时。服务器在启用插件时启动。
代码: 当站点连接到插件时执行。
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");
}
控制台:
[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)
使用 input.close();
关闭 InputReader 时,您也关闭了底层 Socket 连接。
正如您在 AudioClient
,或者使用 DataInputStream
/BufferedInputStream
从中读取数据。