I have been working on test code for a client/server to implement into an existing project.
My question is: could I improve the code to make it run faster, WITHOUT overcomplicated things ?
Once I implement this into my existing code, it will probably have to handle between 10 to 20 updates per second ( several different object types possible. )
***
MAIN
***
public class Main {
public static void main (String[] args){
TestObj to = new TestObj();
Client client = new Client();
Server server = new Server();
Thread t = new Thread(server,"");
t.start();
to.set("Moo");
client.out(to);
to.set("Baa");
client.out(to);
to.set("Quack");
client.out(to);
}
}
***
TestObj ( only used for this code test )
***
public class TestObj implements Serializable {
String s;
public void set(String str){
s = str;
}
public void say(){
System.out.println("You said: " + s);
}
}
***
Client
***
public class Client {
Socket socket;
ObjectInputStream input;
ObjectOutputStream output;
public void out(Object obj){
try{
socket = new Socket ("127.0.0.1",1660);
output = new ObjectOutputStream (socket.getOutputStream());
output.writeObject(obj);
}
catch(Exception e){
System.out.println("Client Crash\n" + e);
}
}
}
***
Server
***
public class Server implements Runnable {
ServerSocket ssocket;
Socket socket;
ObjectInputStream input;
Object dump;
TestObj to;
@Override
public void run() {
try{
ssocket = new ServerSocket(1660);
while (true){
socket = ssocket.accept();
input = new ObjectInputStream(socket.getInputStream());
dump = input.readObject() ;
socket.close();
// if (dump instanceof TestObject){}
to = (TestObj) dump;
to.say();
}
}
catch (Exception e){
System.out.println("Server Crash\n" + e);
}
}
}