// chatserver3.java Use: java -cp . chatserver3

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

class chatserver3 {
 public static void main(String args[]) 
                          throws Exception {
   ServerSocket connection = new ServerSocket( 1025 );
                 // Start separate thread for each
   while(true) new server3(connection.accept());
 }
}

class server3 implements Runnable { 
  Socket s;
 
  server3(Socket s) { 
     this.s = s; 
     new Thread(this).start();   
  }

   public void run() {
      String from;
      BufferedReader in=null;
      PrintStream    out=null;

      try {					
       	in = new BufferedReader(
              new InputStreamReader(s.getInputStream()));
	out = new PrintStream(s.getOutputStream());

       	System.out.println("Connected");
       	while( (from=in.readLine()) != null && 
                !from.equals("")) {
	     System.out.println( from );
	     out.print(from + "\r\n");
       	}
	s.close();                              	
      }
      catch(IOException e) {}
      System.out.println("Disconnected");
   }
}
// chatserver4.java Use: java -cp . chatserver4

import java.net.*;
import java.io.*;
import java.util.Vector;

class chatserver4 {
  public static void main(String args[]) throws Exception
    ServerSocket connection = new ServerSocket( 1025 );

    while(true) new server4(connection.accept());
}

class server4 implements Runnable {
  Socket s;				// common outVector
  static Vector outVector = new Vector();
 
  server4(Socket s) { 
    this.s = s; 
    new Thread(this).start();
  }

  public void run() {
    String from;
    BufferedReader in=null;
    PrintStream    out=null;

    try {
	in = new BufferedReader(
               new InputStreamReader(s.getInputStream()));
             out = new PrintStream(s.getOutputStream());
					
	outVector.addElement(out); 	// add connection

	System.out.println("Connected");     
	while( (from=in.readLine()) != null && 
               !from.equals("")) {
	  System.out.println( from );
					// send all connections
	  for(int i=0; i<outVector.size(); i++)	
	   ((PrintStream)outVector.elementAt(i)).print(from + "\r\n");
        }
	s.close();                              	
     }
     catch(IOException e) {}
     System.out.println("Disconnected");
					
     outVector.removeElement(out); 	// remove connection
   }
}