How to send two objects through a client / server socket? JAVA

0

I have a small problem regarding the serversockets issue and it is that I want to send two objects through socket to the server from the client class which contains two integer variables to make a sum on the server and to return the response. I leave the code of how I am doing the sending and receipt of data

CLIENT CLASS

try{
            Socket ConectarAServidor = new Socket("localhost",1230);
            DesdeServidor = new DataInputStream(ConectarAServidor.getInputStream());
            ParaServidor = new DataOutputStream(ConectarAServidor.getOutputStream());
            Scanner teclado = new Scanner(System.in);
            System.out.println("Introduzca primer numero: ");
            String numeroa = teclado.nextLine();
            int numa = Integer.parseInt(numeroa);
            System.out.println("Introduzca segundo numero: ");
            String numerob = teclado.nextLine();
            int numb = Integer.parseInt(numerob);
            ParaServidor.writeDouble(numa);
            ParaServidor.writeDouble(numb);
            int suma = DesdeServidor.readInt();
            System.out.println("Primer numero: "+numa);
            System.out.println("\nSegundo numero: "+numb);
            System.out.println("Suma recibida: "+suma);
        }catch(Exception e){
            System.err.println(e);
        }

SERVER CLASS

try {
        Socket connectToServer = new Socket("localhost", 1234);
        isFromServer = new DataInputStream(connectToServer.getInputStream());
        osToServer = new DataOutputStream(connectToServer.getOutputStream());
        Scanner teclado = new Scanner(System.in);
        System.out.println("Introduzca el radio: ");
        String cadena = teclado.nextLine();        
        double radius = Double.parseDouble(cadena.trim());
        osToServer.writeDouble(radius);
        osToServer.flush();
        double area = isFromServer.readDouble();
        System.out.println("Radio es "+radius);
        System.out.println("Area recibido desde el servidor es "+area+"\n");
    } catch (Exception e) {
        System.err.println(e);
    }
    
asked by Enrique Jr Batista 03.12.2018 в 01:14
source

0 answers