Reputation: 39
I am trying to connect a website to my java plugin (in minecraft) and let them communicate and play audio. 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)
Upvotes: 0
Views: 612
Reputation: 407
When closing the InputReader using input.close();
, you also close the underlying Socket connection.
As you can see in this question, you cannot close the BufferedReader without closing the Socket. Your only options would be to either pass the BufferedReader to your AudioClient
, or to use a DataInputStream
/BufferedInputStream
to read the data from.
Upvotes: 1