April 05
Prof. Ismael H. F. Santos -  ismael@tecgraf.puc-rio.br                                                          2
Server in Loop: Always up
n// SimpleServerLoop.java: a simple server program that runs forever in a single thead
nimport java.net.*;
nimport java.io.*;
npublic class SimpleServerLoop {
n  public static void main(String args[]) throws IOException {
n    // Register service on port 1234
n    ServerSocket s = new ServerSocket(1234);
n    while(true)
n    {
n            Socket s1=s.accept(); // Wait and accept a connection
n            // Get a communication stream associated with the socket
n            OutputStream s1out = s1.getOutputStream();
n            DataOutputStream dos = new DataOutputStream (s1out);
n            // Send a string!
n            dos.writeUTF("Hi there");
n            // Close the connection, but not the server socket
n            dos.close();
n            s1out.close();
n            s1.close();
n    }
n  }
n}
n