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

class EchoServer
{
    public static final int    ECHO_PORT   = 3737;
    public static final String EXIT_STRING = "***";

    public static void main( String [] args )
    {
        Socket sock = null;

        try
        {
            ServerSocket ss = new ServerSocket( ECHO_PORT );

            sock = ss.accept( );
            System.out.println( "Connected from " + sock.getInetAddress( ) +
                                              ":" + sock.getPort( ) );

            InputStreamReader in = new InputStreamReader( sock.getInputStream( ) );
            BufferedReader is = new BufferedReader( in );
            PrintWriter os = new PrintWriter( sock.getOutputStream( ), true ); // Autoflush 

            os.println( "Welcome to the RudeEchoServer!" );
            os.println( "Enter " + EXIT_STRING + " to exit" );

            String str;

            while( ( str = is.readLine( ) ) != null )
            {
                if( str.trim( ).equals( EXIT_STRING ) )
                    break;
                os.println( str.toUpperCase( ) );
            }
        }
        catch( IOException e )
        {
            System.out.println( e );
        }
        finally
        {
            try { if( sock != null ) sock.close( ); }
            catch( IOException e ) { System.out.println( e ); }
        }
    }
}
