Wednesday, 11 September 2013

Java Networking Console Spam

Java Networking Console Spam

I am trying to make a plugin for Bukkit, that will allow multiple servers
to talk to each other (send data over ip:port). I have got the basic code
working but when I reload the plugin the console gets spammed with this
java.net.SocketException: Socket is closed 2013-09-11 19:56:19 [SEVERE] at
java.net.ServerSocket.accept(Unknown Source) 2013-09-11 19:56:19 [SEVERE]
at com.github.dcsoftgit.multiserver.ReadThread.run(ReadThread.java:14)
This is my code for server1:
package com.github.dcsoftgit.multiserver;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class MultiServer extends JavaPlugin {
public static ServerSocket sock;
ReadThread t;
public void onEnable() {
t = new ReadThread();
try {
sock = new ServerSocket(4571);
} catch (IOException e) {
e.printStackTrace();
}
t.start();
getServer().getScheduler().scheduleSyncDelayedTask(this, new
Runnable() {
@Override
public void run() {
sendDataToSocket("Hello server 2");
}
});
}
public void onDisable() {
try {
sock.close();
} catch (IOException e) {
}
}
public static void sendDataToSocket(String data) {
Socket client;
try {
client = new Socket("127.0.0.1", 4572);
DataOutputStream ds = new
DataOutputStream(client.getOutputStream());
ds.writeUTF(data);
ds.close();
client.close();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
and this is server2:
package com.github.dcsoftgit.multiserver;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class MultiServer extends JavaPlugin {
public static ServerSocket sock;
ReadThread t;
public void onEnable() {
t = new ReadThread();
try {
sock = new ServerSocket(4572);
} catch (IOException e) {
e.printStackTrace();
}
t.start();
getServer().getScheduler().scheduleSyncDelayedTask(this, new
Runnable() {
@Override
public void run() {
sendDataToSocket("Hello server 1");
}
});
}
public void onDisable() {
try {
sock.close();
} catch (IOException e) {
}
}
public static void sendDataToSocket(String data) {
Socket client;
try {
client = new Socket("127.0.0.1", 4571);
DataOutputStream ds = new
DataOutputStream(client.getOutputStream());
ds.writeUTF(data);
ds.close();
client.close();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
this is the ReadThread class:
package com.github.dcsoftgit.multiserver;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class ReadThread extends Thread {
public void run(){
while (!isInterrupted()){
try {
//init the client
Socket client = MultiServer.sock.accept();
//Read the data
DataInputStream dis = new
DataInputStream(client.getInputStream());
String data = dis.readUTF();
System.out.println(data);
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public synchronized void start() {
super.start();
}
}
Using this code when I reload the plugin it calls onDisable() and then
onEnable(). When the server starts it only calls onEnable().

No comments:

Post a Comment