-1

I have a server, and this serves many clients. All clients are connected a server. The server serves each client on a different thread which sends the object to a client. Each one of these threads uses the method returnToken() because this returns a token instance created in main class. The problem is, the client side receives the object (or seems to), but cannot access its attributes. When the client access the attribute of the object, this returns null. Beside this, the object token in entrada class is not used.

public class ThreadServidor extends Thread implements Runnable{

    Socket cliente;

    ThreadServidor(Socket cliente){
        this.cliente = cliente;
    }

    @Override
    public void run() {
        ObjectOutputStream saida = null;
        try {
            saida = new ObjectOutputStream(cliente.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(ThreadServidor.class.getName()).log(Level.SEVERE, null, ex);
        }

        while(true){
        try {
            saida.writeUnshared(Token.returnToken());
            saida.flush();
            saida.reset();

        } catch (IOException ex) {
            Logger.getLogger(ThreadServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
                    Thread.sleep(15000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadServidor.class.getName()).log(Level.SEVERE, null, ex);
                }

        }
    }
}

My class entrada in client side:

public class Entrada implements Runnable {

    private Socket servidor;
    private String id;

    Entrada(Socket cliente, String id){
        this.servidor = cliente;
        this.id = id;
    }

    @Override
    public void run() {

       ObjectInputStream entrada = null;
        try {
            entrada = new ObjectInputStream(servidor.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(Entrada.class.getName()).log(Level.SEVERE, null, ex);
        }

     // Loop principal
        while (true) {
            try {
            try {
                // Recebe mensagem do servidor
                Token token = (Token) entrada.readUnshared();
                System.out.println("Sender is "+Token.returnSender());

            } catch (IOException ex) {
                Logger.getLogger(Entrada.class.getName()).log(Level.SEVERE, null, ex);
            }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Entrada.class.getName()).log(Level.SEVERE, null, ex);
            }        
        }
    } 
}

And the Token Class:

public class Token implements Serializable {
    private static final long serialVersionUID = 1L;

    public static String sender, receiver, content;
    String id;

    public Token(String sender, String receiver, String content){
        Token.sender = sender;
        Token.receiver = receiver;
        Token.content = content;    

    }

    public static Token returnToken(){
        Token token = new Token(Token.sender, Token.receiver, Token.content);

        return token;
    }

    public static String returnSender(){
        System.out.println("Sender is "+sender);
        return Token.sender;
    }

    public boolean hasToken(String id){
        if(Token.sender.equals(id)){
            return true;
        }
        else {return false;}
    }
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • You already asked this question (http://stackoverflow.com/questions/30673755/static-arguments-in-object-send-via-sockets) and you already received an answer telling you what was wrong. You clearly do not understand how `static` variables are intended to be used (e.g. sparingly for singleton-type data). It makes no sense to pass a static member of `Token` to the constructor as an argument only to use it to set that same static member! You need to do a little more studying if you don't understand the valid answer you were given rather than reasking the question. – pens-fan-69 Jun 08 '15 at 15:16

1 Answers1

1

Static fields are not serialized. Make them instance members.

You need to catch EOFException separately and stop reading when you catch it.

user207421
  • 305,947
  • 44
  • 307
  • 483