import java.io.*; import java.net.*; import java.util.* ; import javax.swing.* ; /** * A Connector is a server that listens for I/O connections * at a port. Each client gets a Socket and buffered * i/o streams bundled in a Transducer object. * Text read at any input is rebroadcast to other clients. *
* ===================================== * Copyright 1999-- John R. Fisher * jrfisher@csupomona.edu * ===================================== ** @author jrfisher@csupomona.edu */ public class Connector extends Thread { int clientNum ; int port ; ServerSocket portalSocket ; Vector collaborators ; // Object output streams for clients public Connector(int port) { this.clientNum = 1 ; this.port = port ; this.collaborators = new Vector() ; } public void run() { // Catch big exceptions that prevent server from continuing. try { // 1 portalSocket = new ServerSocket(port) ; while(true) { // Catch smaller exceptions so server itself can continue.. try { // 2 Socket soc = portalSocket.accept() ; BufferedWriter out = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())) ; collaborators.add(out) ; System.out.println("Spawning Transducer for " + clientNum) ; Transducer b = new Transducer(this, new BufferedReader(new InputStreamReader(soc.getInputStream())), out, clientNum) ; b.start() ; clientNum++ ; } catch(Exception e2) { JOptionPane.showMessageDialog(null,e2.toString(),"CONNECTOR EXCEPTION #2",JOptionPane.WARNING_MESSAGE) ; } } } catch (Exception e1) { // Could not make ServerSocket JOptionPane.showMessageDialog(null,e1.toString(),"CONNECTOR EXCEPTION #1",JOptionPane.WARNING_MESSAGE) ; } } /** * From the command line ... * java -classpath