
programa cliente servidor en java que haga operaciones y que grafique dos puntos x, y y,
Publicado por luis (1 intervención) el 22/09/2017 06:19:14
#SERVIDOR#
#CLIENTE#
ME FALTARIA HACER QUE GRAFIQUEE!!!
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
package socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
*
*/
public class Servidor {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
double num1, num2, total = 0.0;
int operacion;
char opr = '\n';
//Crea un socket en el puerto 12342
ServerSocket servidor = new ServerSocket(12342);
System.out.println("Puerto 12342 abierto!");
// Espera que alguien se conecte. La ejecución del servidor
// se bloquea en la llamada al método accept de la clase
// ServerSocket. Cuando alguien se conecta al servidor,
// método desbloquea y devuelve con un objeto de clase
System.out.print("Esperando conexion de un cliente...");
Socket cliente = servidor.accept();
System.out.println("Nueva conexion con el cliente " + cliente.getInetAddress().getHostAddress());
ObjectOutputStream resultado = new ObjectOutputStream(cliente.getOutputStream());
ObjectInputStream datos = new ObjectInputStream(cliente.getInputStream());
operacion = datos.readInt();
num1 = datos.readDouble();
num2 = datos.readDouble();
if (operacion == 1) {
opr = '+';
total = (num1 + num2);
} else if (operacion == 2) {
opr = '-';
total = (num1 - num2);
} else if (operacion == 3) {
opr = 'x';
total = (num1 * num2);
} else {
opr = '/';
total = (num1 / num2);
}
resultado.writeDouble(total);
resultado.writeChar(opr);
resultado.flush();
resultado.close();
datos.close();
servidor.close();
}
}
#CLIENTE#
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
package socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class Cliente {
public static void main(String[] args) throws UnknownHostException, IOException {
double num1;
double num2;
int operacion = 0;
char opr;
Socket cliente = new Socket("127.0.0.1", 12342);
System.out.println("El cliente se conectó al servidor");
ObjectInputStream resultado = new ObjectInputStream(cliente.getInputStream());
ObjectOutputStream datos = new ObjectOutputStream(cliente.getOutputStream());
num1 = Double.parseDouble(JOptionPane.showInputDialog("Digita el primer numero"));
num2 = Double.parseDouble(JOptionPane.showInputDialog("Digita el segundo numero"));
while (!((operacion >= 1) && (operacion <= 4))) {
operacion = Integer.parseInt(JOptionPane.showInputDialog("que operacion deseas hacer?"+"\n"+"1.= SUMA," + " 2. = RESTA," + " 3. = MULTIPLICA," + " 4. = DIVIDE "));
if (!((operacion >= 1) && (operacion <= 4))) {
System.out.println(" operación no válida.");
}
}
datos.writeInt(operacion);
datos.writeDouble(num1);
datos.writeDouble(num2);
datos.flush();
double total = resultado.readDouble();
opr = resultado.readChar();
System.out.println("Total de " + num1 + opr + num2 + " = " + total);
resultado.close();
datos.close();
cliente.close();
}
}
ME FALTARIA HACER QUE GRAFIQUEE!!!
Valora esta pregunta


0