import java.net.*; import java.io.*; public class ValidatedMessageServer extends Thread implements ObjectInputValidation { int port; private String message; private byte[] data; ServerSocket ss; public ValidatedMessageServer() throws IOException { this("Hello", 3452); } public ValidatedMessageServer(String message) throws IOException { this(message, 3452); } public ValidatedMessageServer(String message, int port) throws IOException { this.message = message; data = message.getBytes(); this.port = port; ss = new ServerSocket(port); start(); } public void run() { try { while (true) { Socket s = ss.accept(); OutputStream os = s.getOutputStream(); os.write(data); s.close(); } } catch (IOException e) { } } public synchronized void setMessage(String s) { this.message = message; data = message.getBytes(); } public String getMessage(String s) { return message; } public int getPort(int port) { return port; } public void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(port); out.writeUTF(message); } public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.registerValidation(this, 0); port = in.readInt(); message = in.readUTF(); } public void validateObject() throws InvalidObjectException { data = message.getBytes(); try { ss = new ServerSocket(port); start(); } catch (IOException e) { throw new InvalidObjectException("Could not open server socket on port " + port); } } }