import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;


class Cliente extends JFrame implements ActionListener 
{
    JLabel      labelLeitor;
    JButton     button;
    JPanel      panelLeitor;
    JScrollPane scrollPaneLeitor;
    JTextArea   textAreaLeitor;

    String enderecoServidor;
    int	   porta;           


    Cliente(String enderecoServidor, int porta)
    {
        this.porta = porta;
        this.enderecoServidor = enderecoServidor;

        panelLeitor = new JPanel();
        panelLeitor.setLayout(new BorderLayout());
        panelLeitor.setBackground(Color.white);
        getContentPane().add(BorderLayout.NORTH, panelLeitor);

        button = new JButton("Enviar");
        button.setSize(2, 2); 
        button.addActionListener(this);
        panelLeitor.add("South", button);

        textAreaLeitor = new JTextArea(10, 28);
        textAreaLeitor.setEditable(false); 

        labelLeitor = new JLabel("Chamadas recebidas:");
        panelLeitor.add("Center", labelLeitor);

        scrollPaneLeitor = new JScrollPane(textAreaLeitor);
        panelLeitor.add(scrollPaneLeitor, BorderLayout.CENTER);
    }


    public void actionPerformed(ActionEvent event)
    {
        Object source;
        String texto;
        int tam, id, i, quant;
        Socket socket = null;        
        DataOutputStream out = null; 
        DataInputStream in = null;   

        i = 0;
        source = event.getSource();
        if (source == button)
        {
           try
           {
               java.util.Date d1 = new java.util.Date();
               socket = new Socket(enderecoServidor, porta);
               out = new DataOutputStream(socket.getOutputStream());
               in = new DataInputStream(socket.getInputStream());

               out.writeInt(1);
               texto = "arquivo 1";
               tam = texto.length();
               out.writeInt(tam);
               out.writeBytes(texto);

               tam = in.readInt();
               byte msgLida[] = new byte[tam];
               in.read(msgLida);
               texto = new String(msgLida);
               textAreaLeitor.append(texto + "\n");
               textAreaLeitor.append("\n");

	       quant = 17;
	       id = in.readInt();
               while (id == 0)
               {
                  tam = in.readInt();
                  byte bytes[] = new byte[tam];
                  in.read(bytes);
                  texto = new String(bytes);
                  i = in.readInt();
	          quant = quant + 12 + texto.length();
                  textAreaLeitor.append(texto + i + "\n");

                  id = in.readInt();
               }

               java.util.Date d2 = new java.util.Date();
               System.out.println("Tempo: " + (d2.getTime() - d1.getTime()) + "  total: " + quant + " ultimo: " + i);

               in.close();
               out.close(); 
               socket.close();
           } 
           catch (UnknownHostException e) 
           {
               System.out.println("Host desconhecido");
               System.exit(0);
           } 
           catch  (IOException e) 
           {
               System.out.println("Erro de I/O ao criar o socket");
               System.exit(0);
           }
           catch  (Exception e) 
           {
               System.out.println("Ocorreu uma falha!");
               System.exit(0);
           }
        }
    }

    protected void finalize()
    {
    }


    //recebe como parametro o ip do servidor
    public static void main(String[] args)
    {
        Cliente cliente = new Cliente(args[0], 4447);

	/*Para capturar eventos da janela.
	Ao fechá-la, terminar a aplicação cliente*/
        WindowListener wl = new WindowAdapter() 
                            {
                                public void windowClosing(WindowEvent e) 
                                {
                                    System.exit(0);
                                }
                            };

	/*Adicionar ouvinte de eventos da janela*/
        cliente.addWindowListener(wl);

	/*Ajustar tamanho da janela*/
        cliente.pack();
        cliente.setVisible(true);
    }
}
