import java.io.*;
import java.net.*; 

class CounterServer {
  public static void main(String[] args) {
    System.out.println("CounterServer started."); 
    int i = 1;
    try {
      InputStream fin = new FileInputStream("Counter.dat"); 
      DataInputStream din = new DataInputStream(fin); 
      i = din.readInt() + 1;
      din.close(); 
    } catch (IOException e) {} 

    try {
      ServerSocket s = new ServerSocket(8190); 
      for (;;) {
        Socket incoming = s.accept(); 
        DataOutputStream out 
          = new DataOutputStream(incoming.getOutputStream()); 
        System.out.println("Count: " + i);
        out.writeInt(i); 
        incoming.close(); 
        OutputStream fout = new FileOutputStream("Counter.dat"); 
        DataOutputStream dout = new DataOutputStream(fout); 
        dout.writeInt(i); 
        dout.close(); 
        out.close(); 
        i++;
      }
    } catch (Exception e) {
      System.out.println("Error: " + e); 
    }
    System.out.println("CounterServer stopped."); 
  }

}