Source of CounterServer.java


  1: 
  2: import java.io.*;
  3: import java.net.*; 
  4: 
  5: class CounterServer {
  6:   public static void main(String[] args) {
  7:     System.out.println("CounterServer started."); 
  8:     int i = 1;
  9:     try {
 10:       InputStream fin = new FileInputStream("Counter.dat"); 
 11:       DataInputStream din = new DataInputStream(fin); 
 12:       i = din.readInt() + 1;
 13:       din.close(); 
 14:     } catch (IOException e) {} 
 15: 
 16:     try {
 17:       ServerSocket s = new ServerSocket(8190); 
 18:       for (;;) {
 19:         Socket incoming = s.accept(); 
 20:         DataOutputStream out 
 21:           = new DataOutputStream(incoming.getOutputStream()); 
 22:         System.out.println("Count: " + i);
 23:         out.writeInt(i); 
 24:         incoming.close(); 
 25:         OutputStream fout = new FileOutputStream("Counter.dat"); 
 26:         DataOutputStream dout = new DataOutputStream(fout); 
 27:         dout.writeInt(i); 
 28:         dout.close(); 
 29:         out.close(); 
 30:         i++;
 31:       }
 32:     } catch (Exception e) {
 33:       System.out.println("Error: " + e); 
 34:     }
 35:     System.out.println("CounterServer stopped."); 
 36:   }
 37: 
 38: }
 39: 
 40: