import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class DynMcufClient
{
  private DatagramSocket sock = null;
  private InetAddress host;
  private int port;
  private DynMcufClientSend send;
  private DynMcufClientRecv recv;

  public void start( String hostname, int port, FrameReceiver receiver )
  {
    stop( );
    try
    {
      sock = new DatagramSocket( );
      host = InetAddress.getByName( hostname );
      this.port = port;
      send = new DynMcufClientSend( sock, host, port );
      recv = new DynMcufClientRecv( sock, host, port, receiver );
      recv.start( );
      send.start( );
    }
    catch( SocketException e )
    {
      sock = null;
      host = null;
      port = 0;
    }
    catch( UnknownHostException e )
    {
      sock = null;
      host = null;
      port = 0;
    }
  }

  public void stop( )
  {
    sock = null;
    host = null;
    port = 0;
    if( send != null )
    {
      send.terminate( );
      send = null;
    }
    if( recv != null )
    {
      recv.terminate( );
      recv = null;
    }
  }
}
