Olá pessoal, boa noite, estou há um tempo tentando fazer isso mas acho que o único lugar onde devo conseguir é aqui… estou quebrando a cabeça para fazer uma aplicação que um servidor leia uma string na rede e jogue o dado num jLabel. Quer dizer, conseguir fazer até eu já consegui, mas queria fazer com que ele inicializasse o servidor à partir de uma outra classe, uma com a interface gráfica e outra só com o ServerSocket, ai ele ficaria em execução e a classe principal que roda a parte gráfica lesse o que recebeu e mostrasse no jLabel. Vou colocar os códigos aqui e me desculpem pela minha ignorância se for uma coisa muito ridícula e eu não consegui pensar nisso.
Classe ServerAPI
package Server;
/**
*
* @author Cotts
*/
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServerAPI {
ServerSocket server;
Socket socket;
int port;
BufferedReader in;
public void ServerConect(){
try {
server = new ServerSocket(2000);
while(true){
server.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
in.readLine(); //queria jocar esta informação no label de outra classe.
socket.close();
}
}
catch (IOException ex) {
System.out.println(ex);
}
}
}
Classe ServerGUI
package Server;
import java.io.BufferedReader;
import java.io.IOException;
public class ServerGUI extends javax.swing.JFrame {
public ServerGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Status:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(207, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(223, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) throws IOException {
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 ex) {
java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerGUI().setVisible(true);
}
});
ServerAPI conect = new ServerAPI();
conect.ServerConect();
jLabel2.setText(String.valueOf(conect.in)); // queria jogar nesse jLabel
}
private javax.swing.JLabel jLabel1;
public static javax.swing.JLabel jLabel2;
}
Desculpem pelo excesso de código, pois estou utilizando o Netbeans e ele cira muito texto para a parte gráfica.
Agradeço desde já a ajuda.