import java.io.* ;
import java.net.* ;

public class KlientEcho
{
    public static final int PORT = 2005 ;
    public static final String HOST = "localhost" ;

    public static void main (String[] args)
    {
        System.err.println("- uruchomiono klienta ECHO -") ;
        try
        {
            BufferedReader stdwe = new BufferedReader(new InputStreamReader(System.in)) ;
            PrintWriter stdwy = new PrintWriter(System.out,true) ;
            Socket gniazdo = new Socket(HOST,PORT) ;
            System.err.println("-- otworzono polaczenie (local port: "+
                gniazdo.getLocalPort()+" // remote host: "+
                gniazdo.getInetAddress().getHostName()+" : "+
                gniazdo.getPort()+") --") ;
            BufferedReader we = null ;
            PrintWriter wy = null ;
            try
            {
                we = new BufferedReader(new InputStreamReader(gniazdo.getInputStream())) ;
                wy = new PrintWriter(gniazdo.getOutputStream(),true) ;
                while (true)
                {
                    String linia = stdwe.readLine() ;
                    wy.println(linia) ;
                    if (linia.equals("")) break ;
                    stdwy.println(we.readLine()) ;
                }
            }
            catch (IOException ex) {}
            finally
            {
                if (we!=null) try { we.close() ; } catch (IOException ex) {}
                if (wy!=null) wy.close() ;
            }
            gniazdo.close() ;
            System.err.println("-- zamknieto polaczenie --") ;
        }
        catch (UnknownHostException ex) { System.err.println("- nieznany host -") ; }
        catch (IOException ex) { System.err.println("- blad we/wy -") ; }
        System.err.println("- zatrzymano klienta ECHO -") ;
    }
}
