Problem when reading objects sent by Java Socket

1

I have a problem sending an object of one class per socket. I have the class hiloservidor which is responsible for executing threads every time a request arrives.

private Socket ss;
private int counter;

public hiloservidor(Socket i,int c){
    this.ss=i;
    this.counter=c;
}

@Override
public void run(){

    try{
        boolean done = false;
        System.out.println("hola cliente "+counter);


        while(!done){
            ObjectInputStream entrada = new ObjectInputStream(ss.getInputStream()); 
            ClaseServidor cla = (ClaseServidor) entrada.readObject();--> aquí está el problema se queda en un cliclo infinito en esta linea.

            System.out.println("Datos del servidor de descarga recibido");
        }

    }catch(Exception e){}
}

and I have the client class:

public class SerDes{
    public static void main(String args[]){
        try {

            boolean salir=false;

            int i = 1;
            Socket cliente = new Socket("127.0.0.1",4890);
            ArrayList<String>coleccion = new ArrayList<String>();
            coleccion.add("Libro1");
            coleccion.add("Libro2");
            coleccion.add("Libro3");
            coleccion.add("Libro4");
            coleccion.add("Libro5");
            coleccion.add("Libro6");

            ClaseServidor miServidor = new ClaseServidor(InetAddress.getLocalHost().getHostAddress(),15253,coleccion);

            System.out.println(miServidor.getColeccion());*/

            while(salir==false){
                ObjectOutputStream msgToServer = new ObjectOutputStream(cliente.getOutputStream());
                msgToServer.writeObject(miServidor);
                msgToServer.flush();
                System.out.println("datos del servidor enviados");
                salir = true;
            }
        }
        catch(Exception e){}
    }
}

class ClaseServidor that is in the two projects

public class ClaseServidor implements Serializable{
    String ip;
    int puerto;
    ArrayList<String> coleccion;

    public ClaseServidor(){}

    public ClaseServidor(String ip, int puerto, ArrayList<String> coleccion) {
        super();
        this.ip = ip;
        this.puerto = puerto;
        this.coleccion = coleccion;
    }

    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public int getPuerto() {
        return puerto;
    }
    public void setPuerto(int puerto) {
        this.puerto = puerto;
    }
    public ArrayList<String> getColeccion() {
        return coleccion;
    }
    public void setColeccion(ArrayList<String> coleccion) {
        this.coleccion = coleccion;
    }
}

The problem is when I read the objects in the threadserver class, it stays in a loop reading the object I do not know why. I tried it sending a String and it works but with no objects. Class ClaseServidor is in both projects and is serialized.

    
asked by Antonio 11.01.2017 в 03:28
source

1 answer

1

After seeing your code a bit I think the problem is that the variable done never takes the value true . You initialize it to false in the scope of the block try and I do not see the site where you change it.

    
answered by 11.01.2017 в 12:26