import java.lang.Thread;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;

public class Server_Threaded extends Frame  {

//*****  Single-thread server, accepts multiple messages
//            expected to be run on capefear.cs.unc.edu:8888

MyFrame outerBox;

TextField hostDisplay, portDisplay;
TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button listenButton, quitButton;

public static final String DEFAULT_HOST = "gamma.cs.unc.edu";
public static final int DEFAULT_PORT = 8888;
String host;

// **************

  public Server_Threaded ()  {

    super ( "Server_Threaded " );

  }  // end Server_Threaded constructor

// **************

  public void buildUI ()  {

  try {
    InetAddress here = InetAddress.getLocalHost ();
    host = here.toString();
  }
  catch (UnknownHostException e) { ;}
  hostDisplay = new TextField ( host, 30 );
  portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
  topPanel = new Panel ();
  topPanel.setLayout ( new BorderLayout () );
  topPanel.add ( "North", hostDisplay );
  topPanel.add ( "South", portDisplay );

  logDisplay = new TextArea ( 40, 10 );
  msgDisplay = new TextArea ( 40, 10 );
  middlePanel = new Panel ();
  middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
  middlePanel.add ( logDisplay );
  middlePanel.add ( msgDisplay );

  listenButton = new Button ( "Listen" );
  quitButton = new Button ( "Quit" );
  buttonPanel = new Panel ( );
  buttonPanel.add ( listenButton );
  buttonPanel.add ( quitButton );

  outerBox = new MyFrame ( this, "Server_Threaded" );
  outerBox.add ( "North", topPanel );
  outerBox.add ( "Center", middlePanel );
  outerBox.add ( "South", buttonPanel );

  outerBox.resize ( 350, 400 );
  outerBox.show ();

  }  // end buildUI 

// **************

  public void connectClient ( )  {

  ServerSocket listenSocket;
  Socket connection;
  OutputStream output;
  InputStream input;
  char c;
  String host;
  int port;
  boolean again = true;
  String string;
  int mark;

    if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );
    else port = DEFAULT_PORT;

    try  {

      logDisplay.setText ( "Server started:  listening on port " + port + "\n" );
      listenSocket = new ServerSocket ( port );
      connection=  listenSocket.accept ();
      input = connection.getInputStream ();
      output = connection.getOutputStream ();
      logDisplay.appendText ( "Connection request received\n" );


      while ( again )  {
      msgDisplay.setText ( "" );
      logDisplay.appendText ( "Message, below, received\n" );
      try {
      while ( ((c = (char) input.read () ) != -1 ) && c != '#' ) 
         msgDisplay.appendText ( String.valueOf ( c ) ); 
      }  catch ( IOException except )  {
        except.printStackTrace ();
        return;
      }

      mark = (msgDisplay.getText ()).indexOf ("$" );
      if ( mark == -1 )  {
      string = new String ( (msgDisplay.getText ()).concat("#") );
      }
       else  {
      string = new String ( ((msgDisplay.getText ()).substring(0, mark)).concat("#") );
       again = false;
       }
      logDisplay.appendText ( "Message, below, sent to client \n" );

      for ( int i = 0; i < string.length(); i++ ) {
        output.write ( (int) string.charAt ( i ) );
}
      logDisplay.appendText ( "Message length = " + string.length () + "\n" );
      }  // end while ( again )

      logDisplay.appendText ( "Closing socket\n" );
      connection.close ();

    }  // end try
    catch ( IOException except)  {
      except.printStackTrace ();
    }  // end catch

  }  // end connectClient 

// **************


  public boolean action (Event event, Object arg) {

    if (event.target == listenButton )  {
       this.connectClient ();
      return true;
    }  // end listenButton 

    if (event.target == quitButton )  {

      hide ();
      dispose ();
      System.exit ( 0 );
      return true;

    }  // end quitButton 

    return false;

  }  // end action

// **************

  public boolean handleEvent ( Event event )  {

    if ( event.id == Event.WINDOW_ICONIFY )  {
      hide ();
      dispose ();
      System.exit ( 0 );
    }  // end if

    return super.handleEvent ( event );

  }  // end handleEvent 

// **************

  public static void main ( String [ ] args )  {

    Frame server = new Server_Threaded ();
    server.buildUI ();

  }  // end main

}  // end Server_Second 

// ************************  MyFrame Class ************************

class MyFrame extends Frame  {
  Frame source;

  MyFrame ( Frame a )  {
    super ( );
    source = a;
  }  // end constructor

  MyFrame ( Frame a, String title )  {
    super ( title );
    source = a;
  }  // end constructor

  public boolean action ( Event event, Object object )  {
    if ( source.action ( event, object ) ) return true;
    else return false;
  }  // end action

  public boolean handleEvent ( Event event )  {
    if ( source.handleEvent ( event ) ) return true;
    else return super.handleEvent ( event );
  }  // end handleEvent

}  // end MyFrame

