Repository navigation
Expand file tree
/
Copy pathCliente.java
More file actions
81 lines (80 loc) · 2.88 KB
/
Copy pathCliente.java
File metadata and controls
81 lines (80 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import javax.swing.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
/**
* Clase Cliente que contiene la logica para solicitar monto por medio de una interfaz
* Esta clase utiliza la clase Mensaje para la comunicacion de los datos.
* @author Gabriel Chacon Alfaro
*/
public class Cliente implements ActionListener{
JTextField int_1;
JTextField int_2;
JTextField int_3;
JTextArea area;
JButton send;
Cliente() {
JFrame frame = new JFrame("Interfaz de Cliente");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
area = new JTextArea();
area.setBounds(20,50,340,130);
int_1=new JTextField();
int_1.setBounds(60, 200, 80,20);
int_2=new JTextField();
int_2.setBounds(60, 250, 80,20);
int_3=new JTextField();
int_3.setBounds(60, 300, 80,20);
send=new JButton("Send");
send.setBounds(200, 300,100,40);
send.addActionListener(this);
frame.add(send);
frame.add(int_1);
frame.add(int_2);
frame.add(int_3);
frame.add(area);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
/**
* Este metodo se ejecuta al presionar el boton de la interfaz.
* Obtiene los valores enteros ingresados y los manda al server utilizando la clase Mensaje.
* Al mismo tiempo recibe los datos mandados de vuelta por el server y los muestra en la interfaz.
*/
public void actionPerformed(ActionEvent e){
String valor = int_1.getText();
String porcentaje = int_2.getText();
String peso = int_3.getText();
System.out.println(valor + " " + porcentaje + " " + peso);
final String HOST = "127.0.0.1";
final int PUERTO = 5000;
DataInputStream in;
DataOutputStream out;
Mensaje solicitud = new Mensaje(Integer.parseInt(valor),Integer.parseInt(porcentaje),Integer.parseInt(peso));
area.append(solicitud.toPrint());
try {
Socket sc = new Socket(HOST, PUERTO);
in = new DataInputStream(sc.getInputStream());
out = new DataOutputStream(sc.getOutputStream());
out.writeUTF(solicitud.toString());
String strDelServidor = in.readUTF();
Mensaje msgDelServidor = Mensaje.fromString(strDelServidor);
area.append(msgDelServidor.toPrint());
System.out.println(msgDelServidor.toString());
sc.close();
}
catch (IOException ex) {
System.out.println("Error Cliente");
}
}
/**
* Este metodo main se encarga de instanciar una clase Cliente
*
* @param args - No recibe parametros (vacio)
*/
public static void main(String[] args) {
new Cliente();
}
}