Chat Client-Server Application with Sockets not working

7

I'm doing a project and I got to the part of the communication between the server and the client and I did not get the output I wanted (output image intended).

The goal is for the server and client to communicate with each other. That is, to test this I type a user and pressing the button online that user name appears in the messages received. This method is not attached as it is well done.

The server is not working.

Can anyone explain this NIMBUS (client class) generated by netbeans? I went to Eclipse, is it supposed to be like this?

CLIENTCLASS

publicclassCliente{publicstaticvoidmain(Stringargs[]){try{for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIManager.getInstalledLookAndFeels()){if("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException e) {

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (javax.swing.UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuequeApp().setVisible(true);
            }
        });
    }
}

Server

public class Servidor {
    public static void main(String[] args) {
        SocketServidor socket_servidor = new SocketServidor();
    }
}

Customer Socket

public class SocketCliente {
    private Socket socket;
    private ObjectOutputStream output;

    public Socket conectar(){   
        try {
            this.socket = new Socket("localHost", 8080); 
            this.output = new ObjectOutputStream(socket.getOutputStream()); 
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return socket;
    }

    public void enviar(MensagensChat mensagem){
        try {
            output.writeObject(mensagem);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

Server Socket

public class SocketServidor {
    private ServerSocket servidorSocket;
    private Socket socket;

    private Map<String, ObjectOutputStream> utilizadoresChat = new HashMap<String, ObjectOutputStream>();  

    public SocketServidor(){
        try {
            servidorSocket = new ServerSocket(8080);
            while(true){                            
                socket = servidorSocket.accept();           
                new Thread(new ListennerSocket(socket)).start();    
            }
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    private class ListennerSocket implements Runnable {
        private ObjectOutputStream output;      
        private ObjectInputStream input;

        public ListennerSocket(Socket socket) {
            try {
                output = new ObjectOutputStream(socket.getOutputStream());
                input = new ObjectInputStream(socket.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        @Override
        public void run() {
            MensagensChat mensagem = null;

            try {
                while((mensagem = (MensagensChat) input.readObject() )!= null){ 
                    Accao accao = mensagem.getAccao(); 
                    if(accao.equals(Accao.ONLINE)){
                        conectar(mensagem, output);
                    }else if(accao.equals(Accao.OFFLINE)){

                    }else if(accao.equals(Accao.ENVIAR_UM)){

                    }else if(accao.equals(Accao.ENVIAR_TODOS)){

                    }else if(accao.equals(Accao.CONTACTOS_ONLINE)){

                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void conectar(MensagensChat mensagem, ObjectOutputStream output){   
            enviar_para_um(mensagem, output);
        }

        private void enviar_para_um(MensagensChat mensagem, ObjectOutputStream output){
            try {
                output.writeObject(mensagem);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

MessageChat class

public class MensagensChat implements Serializable {

private String nome;            
private String mensagem;    
private String nomeClienteReceptorMensagem;     
private Set<String> contactosOnline = new HashSet<String>();    
private Accao accao;                                


public String getNomeCliente() {
    return nome;
}
public void setNomeCliente(String nomeCliente) {
    this.nome = nomeCliente;
}
public String getTextoMensagem() {
    return mensagem;
}
public void setTextoMensagem(String textoMensagem) {
    this.mensagem = textoMensagem;
}
public String getNomeClienteMensagemReservada() {
    return nomeClienteReceptorMensagem;
}
public void setNomeClienteMensagemReservada(String nomeClienteMensagemReservada) {
    this.nomeClienteReceptorMensagem = nomeClienteMensagemReservada;
}
public Set<String> getContactosOnline() {
    return contactosOnline;
}
public void setContactosOnline(Set<String> contactosOnline) {
    this.contactosOnline = contactosOnline;
}
public Accao getAccao() {
    return accao;
}
public void setAccao(Accao accao) {
    this.accao = accao;
}
}
    
asked by anonymous 24.11.2015 / 23:50

1 answer

4

Nimbus Look & Feel

Nimbus is a cross-platform visual UI implementation in Java 6.

It is to be an evolution to the themes already existing in Swing and really is more cute than the others, besides presenting better resolution because it is based on vectors and not on bitmaps.

The code with the loop in the main method of the Cliente class, iterates over existing themes in Java, and if Nimbus is available, it tries to activate it in the current program. It seems strange, but this avoids some unexpected error if the theme is not present in the version of Java that is running. This technique appears in a varied way, but it is a convention type commonly used, regardless of whether to use an IDE or not.

Client-Server

I did the archaeological work of reconstituting some classes that were not posted. I do not know exactly what code you have on the screen, but it looks like you're simply missing the method to read the return.

For example, I have not seen anywhere where you get the response from the server. For example:

this.input = new ObjectInputStream(socket.getInputStream()); 

And then:

public MensagensChat ler() {
    try {
        return (MensagensChat) input.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

Code that works

The code below has been modified from the question and tested successfully. Note that you must run the server first and then the client class.

Customer

public class SocketCliente {
    private Socket socket;
    private ObjectOutputStream output;
    private ObjectInputStream input;

    public Socket conectar() {
        try {
            this.socket = new Socket("localHost", 8080);
            this.output = new ObjectOutputStream(socket.getOutputStream());
            this.input = new ObjectInputStream(socket.getInputStream());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }

    public void enviar(MensagensChat mensagem) {
        try {
            output.writeObject(mensagem);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MensagensChat ler() {
        try {
            return (MensagensChat) input.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String args[]) {

        SocketCliente cliente = new SocketCliente();
        cliente.conectar();
        cliente.enviar(new MensagensChat(Accao.ONLINE, "eu mesmo"));
        MensagensChat retorno = cliente.ler();
        System.out.printf(retorno.getMensagem());


    }
}

Server

public class SocketServidor {
    private ServerSocket servidorSocket;
    private Socket socket;

    private Map<String, ObjectOutputStream> utilizadoresChat = new HashMap<String, ObjectOutputStream>();

    public SocketServidor() {
        try {
            servidorSocket = new ServerSocket(8080);
            while (true) {
                socket = servidorSocket.accept();
                new Thread(new ListennerSocket(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class ListennerSocket implements Runnable {
        private ObjectOutputStream output;
        private ObjectInputStream input;

        public ListennerSocket(Socket socket) {
            try {
                output = new ObjectOutputStream(socket.getOutputStream());
                input = new ObjectInputStream(socket.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        @Override
        public void run() {
            MensagensChat mensagem = null;

            try {
                while ((mensagem = (MensagensChat) input.readObject()) != null) {
                    Accao accao = mensagem.getAccao();
                    if (accao.equals(Accao.ONLINE)) {
                        conectar(mensagem, output);
                    } else if (accao.equals(Accao.OFFLINE)) {

                    } else if (accao.equals(Accao.ENVIAR_UM)) {

                    } else if (accao.equals(Accao.ENVIAR_TODOS)) {

                    } else if (accao.equals(Accao.CONTACTOS_ONLINE)) {

                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void conectar(MensagensChat mensagem, ObjectOutputStream output) {
            enviar_para_um(mensagem, output);
        }

        private void enviar_para_um(MensagensChat mensagem, ObjectOutputStream output) {
            try {
                output.writeObject(mensagem);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new SocketServidor();
    }
}
    
25.11.2015 / 01:07