Então a classe "observer" seria minha classe Server e a classe "processo" seria o meu ClientThread ? E a classe GUI ,como ficaria ?
O negócio complicado! ....kkk obrigado pela atenção , abraços
Tentei implementar , olha como ta ficando :
Classe Server , que fiz o que voce falou , coloquei dentro de um metodo.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pservidor;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;
public class Server {
//
// public static void main(String args[]) throws Exception {
public String startar () throws IOException{
int port = 1234;
ServerSocket server = new ServerSocket(port);
Socket request = null;
Thread client = null;
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();
System.out.println("Servidor inciado no ip! -> "+ip);
while(true){
request = server.accept();
client = new Thread(new ClientThread(this));
client.start();
return ip;
}
}
public void desligar (String s2) throws IOException{
int port = 1234;
ServerSocket server = new ServerSocket(port);
Socket request = null;
ClientThread client = null;
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();
System.out.println("Servidor desligado no ip! -> "+ip);
while(true){
request = server.accept();
client = new ClientThread(request);
client.stop();
}
}
}
Esta dando erro nessa parte da Classe Servidor:
client = new Thread(new ClientThread(this));
Classe ClientThread :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pservidor;
import java.io.DataInputStream;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;
class ClientThread extends Observable implements Runnable{
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
public ClientThread(Observer observador){
addObserver(observador);
}
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
public void run() {
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
String mensagem = in.readUTF();
String login[] = new String[2];
login = mensagem.split("#");
System.out.println("Servidor-> Recebeu Nome : "
+login[0] );
System.out.println("Servidor-> Recebeu Mesa : "
+ login[1] );
System.out.println("Servidor -> Recebeu Pedido : "+login[2]);
System.out.println("Servidor ------> Pedido Finalizado ");
notifyObservers(new String (login[0]));
notifyObservers(new String (login[1]));
notifyObservers(new String (login[2]));
setChanged();
} catch (Exception e) {
e.printStackTrace();
}
try {
socket.close();
notifyObservers(new Boolean(true));
setChanged();
} catch (Exception e) {
System.out
.println("A problem was observed while closing the connection");
}
}
}
Classe GUI:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pservidor;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.io.IOException;
import pservidor.Server;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
*
* @author lucas
*/
public class Principal extends javax.swing.JFrame {
private String[] args;
/**
* Creates new form Principal
*/
public Principal() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jDesktopPane1 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Servidor");
jButton1.setText("Listar Conectados");
jButton2.setText("Conectar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton3.setText("Desconectar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Limpar");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
jLabel1.setForeground(new java.awt.Color(204, 204, 204));
jLabel1.setText("Painel Servidor");
jLabel1.setBounds(120, 10, 160, 23);
jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pservidor/servidor.png"))); // NOI18N
jLabel2.setText("jLabel2");
jLabel2.setBounds(-10, 0, 430, 50);
jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
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(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton3))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(164, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(129, 129, 129))
.addComponent(jDesktopPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1)
.addComponent(jButton3))
.addGap(31, 31, 31)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(jButton4)
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Server s1 = new Server();
s1.startar();
} catch (IOException ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// try {
// Server s1 = new Server();
// s1.desligar(null);
//
// } catch (IOException ex) {
// Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
// }
//
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
limpar();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
public void limpar (){
jTextArea1.setText("");
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}