Java - Suma servidor cliente sockets

 
Vista:
sin imagen de perfil

Suma servidor cliente sockets

Publicado por Simon (20 intervenciones) el 11/10/2017 02:18:55
Hola tengo las clases cliente y servidor quisera que se pidieran dos numeros en el lado del cliente y el servidor realice esta suma y la devuelva a el cliente como se podria hacer ? porfavor ayuda?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;
import java.net.*;
class Cliente {
static final String HOST = "localhost";
static final int PUERTO=5000;
public Cliente( ) {
try{
Socket skCliente = new Socket( HOST , Puerto );
InputStream aux = skCliente.getInputStream();
DataInputStream flujo = new DataInputStream( aux );
System.out.println( flujo.readUTF() );
skCliente.close();
} catch( Exception e ) {
System.out.println( e.getMessage() );
 
}
}
public static void main( String[] arg ) {
new Cliente();
}
}

Y tengo el programa servidor

import java.io.* ;
import java.net.* ;
class Servidor {
static final int PUERTO=5000;
public Servidor( ) {
try {
ServerSocket skServidor = new ServerSocket(PUERTO);
System.out.println("Escucho el puerto " + PUERTO );
for ( int numCli = 0; numCli < 3; numCli++; ) {
Socket skCliente = skServidor.accept(); // Crea objeto
System.out.println("Sirvo al cliente " + numCli);
OutputStream aux = skCliente.getOutputStream();
DataOutputStream flujo= new DataOutputStream( aux );
flujo.writeUTF( "Hola cliente " + numCli );
skCliente.close();
}
System.out.println("Demasiados clientes por hoy");
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}
public static void main( String[] arg ) {
new 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