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;
}
}