/****************teste teste*************
    Universidade Estácio de Sá
    Trabalho BSRC
    Fase: prototipo 01 e 02 - 04/02/2003
    Professor Orientador: Valentim

    Grupo: Julio, Daniel, Claudia, Juceliana
    Date inicial: 04/09/2002
    Date Final:
**/
import java.io.*;
import java.net.*;


class Cliente {
   String msg;
   private ServerSocket serverSocket;
   String line;
   static String Omsg;


public static void main (String args[ ] ) {

	Cliente cli;
	cli = new Cliente();

	DataInputStream lemsg;
	DataInputStream asg;
	//DataInputStream msg;

	lemsg = new DataInputStream(System.in);

	// Abre o Socket

try{
	    Socket s = new Socket("Localhost",6789);



 // Escolhe a opção de digitar a mensagem ou ler um arquivo

    System.out.println("Digite mm para mensagem ou aa para arquivo....");

    try {
			DataInputStream msg;
			asg = new DataInputStream(System.in);
			cli.Omsg = asg.readLine();

		 }catch (IOException i) {
			     System.out.println("Erro !");

	     }

	 //Caso a opção seja digitar a mensagem......
    String str1 = "mm";
    if (Omsg.equals (str1))
    {

    System.out.println("Entre com uma mensagem....");

    try {
				 DataInputStream msg;
				 msg = new DataInputStream(System.in);
			     cli.msg = lemsg.readLine();
	    }catch (IOException i) {
 		           System.out.println("Erro !");
	     }



    // Enviando a mensagem ao Servidor...

			try {

   					//Socket s = new Socket("Localhost",6789);
   					PrintStream out;
   					out = new PrintStream(s.getOutputStream( ));
   					out.println(cli.msg);
			    }
			catch (IOException e)
			  {
			     System.out.println("Error: " + e);
		      }
   }
    // Caso a opção seja ler um arquivo......

    String str2 = "aa";
    if (Omsg.equals (str2))
	{

	try {

         // Socket s = new Socket("Localhost",6789);

          PrintStream out;
          out = new PrintStream(s.getOutputStream( ));
	      //out.println(cli.line);
          //System.out.println(cli.line);

          FileReader file = new
	      FileReader("porra.txt");
	      BufferedReader buff = new
	      BufferedReader(file);
	      boolean eof = false;
          String teste="";
	      while (!eof)
	      {

			  cli.line = buff.readLine();
              out.println(cli.line);

		      if (cli.line == null)
		          eof = true;


		      //else
			      //System.out.println(cli.line); Para imprimir o mesmo arquivo localmente

	       }
	       buff.close();

         //s.close( );
         }catch (IOException i) {
         System.err.println (i.toString( ));
         }
	 }


	 //Recebe a mensagem do Servidor


	 	     System.out.println("Resp:");
	 	     DataInputStream dis = new DataInputStream(s.getInputStream());
	 	     String msg = dis.readLine();
	 	     System.out.println(msg);


		}
			catch(IOException e){
			    System.err.println("Exception: couldn't create socket");
			    System.exit(1);
			}

    }

}

