práctica-04

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

División de Ingenierı́a Informática

Programación en Ambiente Cliente-Servidor

‘‘Práctica # 4’’

Profesor:
Mtro. Gabriel Dı́az Mirón Mac Donough

1 / 20
Práctica # 4

Objetivo
Elaborar una aplicación distribuida que nos permita realizar operaciones
aritméticas básicas dados 2 números cualesquiera utilizando la
tecnologı́a de ‘‘sockets orientados a conexión’’ implementados con el
lenguaje de programación ‘‘Java’’.

Estructura de la aplicación

Programa ‘‘servidor’’
Servidor.java
Servicio.java
Operacion.java
Programa ‘‘cliente’’
Operacion.java
JFrameCliente.java
TestJFrameCliente.java

2 / 20
Modelo conceptual

3 / 20
Cliente gráfico

4 / 20
Servidor.java

1 /* Servidor . java */
2
3 package mipaquete ;
4
5 import java . io .*;
6 import java . net .*;
7
8 class Servidor {
9 public static void main ( String [] args ) {
10 try {
11 ServerSocket servidor = new ServerSocket (8088);
12 for (;;) {
13 Socket cliente = servidor . accept ();
14 new Thread ( new Servicio ( cliente )). start ();
15 }
16 } catch ( IOException exc ) {
17 System . out . println ( exc . getMessage ());
18 }
19 }
20 }

5 / 20
Servicio.java

1 // Servicio . java
2
3 package mipaquete ;
4
5 import java . io .*;
6 import java . net .*;
7
8 public class Servicio implements Runnable {
9 private static Socket cliente ;
10 Servicio ( Socket cliente ) {
11 Servicio . cliente = cliente ;
12 }
13 @Override
14 public void run () {
15 char ch = ’ \0 ’;
16 double n1 = 0.0 d ;
17 double n2 = 0.0 d ;
18 double rs = 0.0 d ;

6 / 20
Servicio.java

19 try {
20 O b j e c t I n p u t S t r e a m entrada = new O b j e c t I n p u t S t r e a m (
21 cliente . getInputStream ()
22 );
23 try {
24 Object Z = entrada . readObject ();
25 if ( Z instanceof Operacion ) {
26 Operacion Y = ( Operacion ) Z ;
27 ch = Y . getOperador ();
28 n1 = Y . getOperando_01 ();
29 n2 = Y . getOperando_02 ();
30 }
31 } catch ( C l a s s N o t F o u n d E x c e p t i o n exc ) {
32 System . out . println ( exc . getMessage ());
33 return ;
34 }
35 System . out . println ( " Operador : " + ch );
36 System . out . println ( " Operando # 1: " + n1 );
37 System . out . println ( " Operando # 2: " + n2 );

7 / 20
Servicio.java

38 switch ( ch ) {
39 case ’+ ’: rs = n1 + n2 ;
40 break ;
41 case ’ - ’: rs = n1 - n2 ;
42 break ;
43 case ’* ’: rs = n1 * n2 ;
44 break ;
45 case ’/ ’: rs = n1 / n2 ;
46 break ;
47 }
48 DataOutputStream dout = new DataOutputStream (
49 new B u f f e r e d O u t p u t S t r e a m (
50 cliente . getOutputStream ()
51 )
52 );
53 dout . writeUTF ( rs + " " );
54 dout . flush ();
55 } catch ( IOException exc ) {
56 System . out . println ( exc . getMessage ());
57 return ;
58 }
59 }
60 }

8 / 20
Operacion.java

1 // Operacion . java
2
3 package mipaquete ;
4
5 import java . io . Serializable ;
6
7 public class Operacion implements Serializable {
8 private char operador ;
9 private double operando_01 ;
10 private double operando_02 ;
11 public Operacion () {
12 this . operador = ’ \0 ’;
13 this . operando_01 = 0.0 d ;
14 this . operando_02 = 0.0 d ;
15 }
16 public Operacion ( char operador , double operando_01 ,
17 double operando_02 ) {
18 this . operador = operador ;
19 this . operando_01 = operando_01 ;
20 this . operando_02 = operando_02 ;
21 }

9 / 20
Operacion.java

22 public void setOperador ( char operador ) {


23 this . operador = operador ;
24 }
25 public void setOperando_01 ( double operando_01 ) {
26 this . operando_01 = operando_01 ;
27 }
28 public void setOperando_02 ( double operando_02 ) {
29 this . operando_02 = operando_02 ;
30 }
31 public char getOperador () {
32 return operador ;
33 }
34 public double getOperando_01 () {
35 return operando_01 ;
36 }
37 public double getOperando_02 () {
38 return operando_02 ;
39 }
40 }

10 / 20
JFrameCliente.java

1 // JFrameCliente . java
2
3 package mipaquete ;
4
5 import java . awt .*;
6 import java . awt . event .*;
7 import java . io .*;
8 import java . net .*;
9 import javax . swing .*;
10
11 public class JFrameCliente extends JFrame implements
12 ActionListener {
13 private JLabel jLabel1 , jLabel2 ;
14 private JTextField jTextField1 , jTextField2 , jTextField3 ;
15 private JButton jButton1 , jButton2 ;
16 private JRadioButton jRadioButton1 , jRadioButton2 ;
17 private JRadioButton jRadioButton3 , jRadioButton4 ;
18 private ButtonGroup ButtonGroup1 ;

11 / 20
JFrameCliente.java

19 public JFrameCliente ( String title ) {


20 super ( title );
21 // construir radio - botones
22 jRadioButton1 = new JRadioButton ( " Sumar " );
23 jRadioButton2 = new JRadioButton ( " Restar " );
24 jRadioButton3 = new JRadioButton ( " Multiplicar " );
25 jRadioButton4 = new JRadioButton ( " Dividir " );
26 // agrupar radio - botones
27 ButtonGroup1 = new ButtonGroup ();
28 ButtonGroup1 . add ( jRadioButton1 );
29 ButtonGroup1 . add ( jRadioButton2 );
30 ButtonGroup1 . add ( jRadioButton3 );
31 ButtonGroup1 . add ( jRadioButton4 );
32 // construir etiquetas y campos de texto
33 jLabel1 = new JLabel ( " Introduzca la cantidad # 1 " );
34 jTextField1 = new JTextField (25);
35 jTextField1 . s e t H o r i z o n t a l A l i g n m e n t ( SwingConstants . CENTER );
36 jLabel2 = new JLabel ( " Introduzca la cantidad # 2 " );
37 jTextField2 = new JTextField (25);
38 jTextField2 . s e t H o r i z o n t a l A l i g n m e n t ( SwingConstants . CENTER );

12 / 20
JFrameCliente.java

39 // construir botones y campos de texto


40 jButton1 = new JButton ( " Aceptar " );
41 jButton1 . a d d A c t i o n L i s t e n e r ( this );
42 jTextField3 = new JTextField (25);
43 jTextField3 . s e t H o r i z o n t a l A l i g n m e n t ( SwingConstants . CENTER );
44 jTextField3 . setEditable ( false );
45 jButton2 = new JButton ( " Limpiar " );
46 jButton2 . a d d A c t i o n L i s t e n e r ( this );
47 s e t D e f a u l t C l o s e O p e r a t i o n ( EXIT_ON_CLOSE );
48 }

13 / 20
JFrameCliente.java

49 public void muestra ()


50 {
51 // agregar los componentes al contenedor
52 Container contentPane = getContentPane ();
53 contentPane . setLayout ( new FlowLayout ());
54 contentPane . add ( jRadioButton1 );
55 contentPane . add ( jRadioButton2 );
56 contentPane . add ( jRadioButton3 );
57 contentPane . add ( jRadioButton4 );
58 contentPane . add ( jLabel1 );
59 contentPane . add ( jTextField1 );
60 contentPane . add ( jLabel2 );
61 contentPane . add ( jTextField2 );
62 contentPane . add ( jButton1 );
63 contentPane . add ( jTextField3 );
64 contentPane . add ( jButton2 );
65 }

14 / 20
JFrameCliente.java

66 public void actionPerformed ( ActionEvent ae ) {


67 boolean flag = false ;
68 if ( jRadioButton1 . isSelected ()== true ) flag = true ;
69 if ( jRadioButton2 . isSelected ()== true ) flag = true ;
70 if ( jRadioButton3 . isSelected ()== true ) flag = true ;
71 if ( jRadioButton4 . isSelected ()== true ) flag = true ;
72 if ( flag == false ) {
73 JOptionPane . s h o w M e s s a g e D i a l o g ( null ,
74 " Seleccione alguna operaci \ u00F3n del men \ u00FA ... " );
75 return ;
76 }
77 JButton jb =( JButton ) ae . getSource ();
78 String txb = jb . getText ();
79 if ( txb . equals ( " Aceptar " )) seleccionar ();
80 if ( txb . equals ( " Limpiar " )) limpiar ();
81 }

15 / 20
JFrameCliente.java

82 public void seleccionar () {


83 java . lang . Double A , B , C =0.0 d ;
84 String s1 = jTextField1 . getText ();
85 String s2 = jTextField2 . getText ();
86 try {
87 A = Double . parseDouble ( s1 );
88 } catch ( N u m b e r F o r m a t E x c e p t i o n exc1 ) {
89 JOptionPane . s h o w M e s s a g e D i a l o g ( null ,
90 " Introduzca la primera cantidad num \ u00E9rica ... " );
91 return ;
92 }
93 try {
94 B = Double . parseDouble ( s2 );
95 } catch ( N u m b e r F o r m a t E x c e p t i o n exc2 ) {
96 JOptionPane . s h o w M e s s a g e D i a l o g ( null ,
97 " Introduzca la segunda cantidad num \ u00E9rica ... " );
98 return ;
99 }
100 socket_cliente (A , B );
101 }

16 / 20
JFrameCliente.java

102 public void limpiar () {


103 jTextField1 . setText ( " " );
104 jTextField2 . setText ( " " );
105 jTextField3 . setText ( " " );
106 }
107 public void socket_cliente ( double a , double b ) {
108 Operacion X = new Operacion ();
109 if ( jRadioButton1 . isSelected ()== true ) X . setOperador ( ’+ ’ );
110 if ( jRadioButton2 . isSelected ()== true ) X . setOperador ( ’ - ’ );
111 if ( jRadioButton3 . isSelected ()== true ) X . setOperador ( ’* ’ );
112 if ( jRadioButton4 . isSelected ()== true ) X . setOperador ( ’/ ’ );
113 X . setOperando_01 ( a );
114 X . setOperando_02 ( b );
115 Socket socket ;
116 String str ;
117 try {
118 socket = new Socket ( " 127.0.0.1 " ,8088);
119 O b j e c t O u t p u t S t r e a m salida = new O b j e c t O u t p u t S t r e a m (
120 socket . getOutputStream ()
121 );

17 / 20
JFrameCliente.java

122 salida . writeObject ( X );


123 salida . flush ();
124 DataInputStream din = new DataInputStream (
125 new B u f f e r e d I n p u t S t r e a m ( socket . getInputStream ())
126 );
127 str = din . readUTF ();
128 jTextField3 . setText ( str );
129 socket . close ();
130 } catch ( IOException ioe ) {
131 System . out . println ( " No hay conexi ó n con el servidor ! " );
132 }
133 }
134 }

18 / 20
TestJFrameCliente.java

1 // T e s t J F r a m e C l i e n t e . java
2
3 package mipaquete ;
4
5 class T e s t J F r a m e C l i e n t e {
6 public static void main ( String [] args ) {
7 JFrameCliente obj = new JFrameCliente (
8 " Operaciones aritm \ u00E9ticas ... "
9 );
10 obj . setSize (340 ,245);
11 obj . setResizable ( false );
12 obj . setVisible ( true );
13 obj . muestra ();
14 }
15 }

19 / 20
Compilación y salida de resultados

20 / 20

You might also like