class ServerOne{ ServerSocket server = null; ... ServerOne(int port){ System.out.println("Starting server on port "+port); try{ server = new ServerSocket(port); System.out.println("Server started successfully and now waiting for client"); } catch (IOException e) { System.out.println("Could not listen on port "+port); System.exit(-1); } } public void listenSocket(){ while(true){ ClientWorker w; try{ w = new ClientWorker(server.accept()); Thread t = new Thread(w); t.start(); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); } } } protected void finalize(){ try{ server.close(); } catch (IOException e) { System.out.println("Could not close socket"); System.exit(-1); } } } class ClientWorker implements Runnable{ Socket client; ClientWorker(Socket client){ this.client = client; } public void run(){ ... sendCommand(parameter); ... } public void sendCommand(String command){ PrintWriter out = null; try { out = new PrintWriter(client.getOutputStream(), true); out.println(command); } catch (IOException ex) {} } }