Redes - Sockets Redes

 
Vista:
sin imagen de perfil

Sockets Redes

Publicado por lORENA (3 intervenciones) el 13/10/2017 03:07:15
Estoy realizando una suma con sockets, donde se pide la suma por el lado del cliente, el servidor recibe los números, realiza la suma y le devuelve el resultado al cliente.
Hice el programa pero me bota error en el momento de dar el resultado de la suma!! Alguien me puede ayduar!!

Clase Servidor

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
package servidorsuma;
 
import java.net.*;
import java.io.*;
 
/**
 *
 * @author HP
 */
 
class ServidorSuma {
 
    public static void main(String []xf){
int x,y,sum;
String a,b;
ServerSocket ss=null;
try{
ss=new ServerSocket(5051);
}
catch(IOException e){}
try{
    Socket s1=ss.accept();
    InputStream is=s1.getInputStream();
    DataInputStream dis=new DataInputStream(is);
    a=dis.readUTF();
    System.out.println("el numero recibido es:"+a);
 
    Socket s2=ss.accept();
    InputStream is2=s2.getInputStream();
    DataInputStream dis2=new DataInputStream(is2);
    b=dis2.readUTF();
    System.out.println("el numero recibido es:"+b);
 
    x=Integer.parseInt(a);
    y=Integer.parseInt(b);
 
    sum=x+y;
 
    Socket sc=new Socket("localhost",5052);
    OutputStream os=sc.getOutputStream();
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("la suma total es:" + sum);
 
dos.close();
s1.close();
s2.close();
 
}
catch(IOException e){}
 
}
}

Clase 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
50
51
52
53
54
55
package clientesuma;
import java.net.*;
import java.io.*;
/**
 *
 * @author HP
 */
public class ClienteSuma {
 
 public static void main(String []xf){
ServerSocket ss=null;
try{
 
ss=new ServerSocket(5052);
 
System.out.println("escriba los numeros para enviarlos con el servidor");
 
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
 
String cad1,cad2;
 
 
cad1=bf.readLine();
 
    Socket sc1=new Socket("localhost",5051);
    OutputStream os1=sc1.getOutputStream();
    DataOutputStream dos1=new DataOutputStream(os1);
    dos1.writeUTF(cad1);
 
cad2=bf.readLine();
 
    Socket sc2=new Socket("localhost",5051);
    OutputStream os2=sc2.getOutputStream();
    DataOutputStream dos2=new DataOutputStream(os2);
    dos2.writeUTF(cad2);
 
    Socket s1=ss.accept();
    InputStream is=s1.getInputStream();
    DataInputStream dis=new DataInputStream(is);
    System.out.println(dis.readUTF());
 
    dis.close();
    s1.close();
    sc1.close();
    dos1.close();
    sc2.close();
    dos2.close();
}
 
catch(IOException e){
    System.out.println("Error: no se encontro el servidor");
}
 
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder