7
I’m doing a project and I got to the communication part between the server and the client and I couldn’t get the output I wanted (image output).
The goal is for the server and client to communicate with each other. That is, to test this I type a user and if I press the online button that user name appears in the received messages. This method is not attached because it is well done.
The server is not working.
And can someone explain this NIMBUS (client class) generated by netbeans? I went to Eclipse, it’s supposed to be like this?
CUSTOMER CLASS
public class Cliente {
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : 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();
}
}
Socketcliente
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();
}
}
}
Socketserver
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();
}
}
}
}
Mensagenschat 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;
}
}
Thank you so much for the answer! Those classes you sent me if you modify them I get the problem solved?
– rrr
I also added my Messaging Chat class here. It’s that I get errors in the Client class implemented by Voce! Thanks
– rrr
If you import my Messaging Chat class and see the constructor of me errors because?
– rrr
I added one more class, is that with the solution this gives me error in several points and I’m not even finding the solution can help me sff? You’re not even giving me the intended output
– rrr
Another question I have is with your solution, I run the server and then the client, so that I have the Mains in the socketClient and socketServitor classes
– rrr