Problem sending objects by Java socket

-1

I have to create two client-server applications to send a person type object from the server to the client. The problem is that reading it in the client gives error because it has not found the class. I put the code, the truth that I do not give with the error. The person class is in the two projects

Person class:

public class Persona implements Serializable {

private String nombre;
private int edad;


public Persona (String nombre, int edad){
    super();
    this.nombre=nombre;
    this.edad=edad;
}
public String getNombre (){
    return nombre;
} 
public void setNombre(String n){
    this.nombre=n;
}
public int getEdad (){
    return edad;
}
public void setEdad (int a){
    this.edad=a;
}

Server class:

public class Servidor {

static final int Puerto=1500;

public Servidor(){

    try {
        // Inicio el servidor en el puerto
        ServerSocket skServidor = new ServerSocket(Puerto);

        System.out.println("Escucho el puerto " + Puerto );

        Socket skCliente = skServidor.accept(); // Crea objeto            

        // Creo el flujo de salida             
        ObjectOutputStream flujo_salida= new ObjectOutputStream(skCliente.getOutputStream());            

        // Creo el flujo de entrada            
        ObjectInputStream flujo_entrada= new ObjectInputStream(skCliente.getInputStream());

        System.out.println("Cliente conectado"); 
        Persona persona=new Persona("Pablo",35);            

        flujo_salida.writeObject(persona);      

        // Se cierra la conexión
        skCliente.close();
        flujo_entrada.close();
        flujo_salida.close();
        System.out.println("Cliente desconectado");
    } catch (IOException ex) {
        Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
    } 

}

Client Class:

public class Cliente {

static final String HOST = "localhost";
static final int Puerto=1500;

public Cliente( ) {

    Persona persona=null;        

    try{            
        // Me conecto al puerto
        Socket skCliente = new Socket( HOST , Puerto );              

        // Creo el flujo de salida             
        ObjectOutputStream flujo_salida= new ObjectOutputStream(skCliente.getOutputStream());             

        // Creo el flujo de entrada            
        ObjectInputStream flujo_entrada= new ObjectInputStream(skCliente.getInputStream());         

        System.out.println("Conectado al servidor");

        persona=(cliente.Persona) flujo_entrada.readObject();      




        // Se cierra la conexión
        skCliente.close();
        flujo_entrada.close();
        flujo_salida.close();
        System.out.println("Cliente desconectado");            

    } catch( Exception e ) {
            System.err.println( e.getMessage()+" "+e.toString() );                
    }
}
    
asked by Carlos 11.05.2018 в 09:43
source

1 answer

0

The error that appears in the line of code is class not fount. I have already found the problem, after searching the internet and the other problem is that when a class is created, it puts a header at the beginning and this is formed by the name of the package point name of the class. In my case:

customer.Person

server.Person

If now we change the name of these packages and we put the same, problem solved. Another option, although more complicated, is to create our own ObjectOutputStream class, inheriting from the original and redefining the writeStreamHeader () method so that it does nothing:

protected void writeStreamHeader () throws IOException { // Do nothing. }

Another option is to create a new package that is named equal within each project with the class that is shared

Also, you can create a jar that contains the Person class and then add that jar to the BUIL PATH in both the client project and the server project

    
answered by 13.05.2018 в 10:32