aí vai o código do meu programa:
------------------------------CLASSE CLIENTE----------------------------
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
/**
*
-
@author LIDI_LINDA
*/
public class Cliente{/** Creates a new instance of Cliente */
public Cliente() {
}public PrintStream Cliente(String saida){
Socket s = null;//Declaro a Stream de saida de dados PrintStream ps = null; try{ //Cria o socket com o recurso desejado na porta especificada s = new Socket("127.0.0.1",8000); //Cria a Stream de saida de dados ps = new PrintStream(s.getOutputStream()); ps.println(saida); //Imprime uma linha para a stream de saída de dados //Trata possíveis exceções }catch(IOException e){ e.printStackTrace(); }finally{ try{ //Encerra o socket cliente s.close(); }catch(IOException e){} }System.out.println(ps);
return ps;
}
}
------------------------------Classe Servidora------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
-
@author LIDI_LINDA
*/
public class Servidor {/** Creates a new instance of Servidor */
public Servidor() {
}
public static String StartServer(int porta){
// SERVIDOR
ServerSocket serv=null;
//Declaro o Socket de comunicação
Socket s= null;
//Declaro o leitor para a entrada de dados
BufferedReader entrada=null;
// String de Saida
String saida = "";
try{
//Cria o ServerSocket na porta se estiver disponível
serv = new ServerSocket(porta);
//Aguarda uma conexão na porta especificada e cria retorna o socket que irá comunicar com o cliente
s = serv.accept();
System.out.println("startado");
//Cria um BufferedReader para o canal da stream de entrada de dados do socket s
entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));
//Aguarda por algum dado e imprime a linha recebida quando recebe
saida = entrada.readLine();
//trata possíveis excessões de input/output. Note que as excessões são as mesmas utilizadas para as classes de java.io
}catch(IOException e){
//Imprime uma notificação na saída padrão caso haja algo errado.
// System.out.println("Algum problema ocorreu para criar ou receber o socket.");
}finally{
try{
//Encerro o socket de comunicação
s.close();
//Encerro o ServerSocket
serv.close();
}catch(IOException e){
}
}
return saida;
}
-----------------------------------Classe Tela-------------------------------------
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
*
-
@author LIDI_LINDA
/
public class SisAlarmeTela extends javax.swing.JFrame {
private static boolean flag = false;
/* Creates new form SisAlarmeTela */
public SisAlarmeTela() {
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.
*/
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jSeparator2 = new javax.swing.JSeparator();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jDesktopPane1 = new javax.swing.JDesktopPane();
jTextField1 = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“Sistema de Alarme vers\u00e3o 1.0”);
setExtendedState(600);
setMaximizedBounds(new java.awt.Rectangle(200, 300, 600, 700));
jLabel1.setFont(new java.awt.Font(“Times New Roman”, 1, 12));
jLabel1.setText(“MARINHA DO BRASIL”);jLabel2.setFont(new java.awt.Font(“Times New Roman”, 1, 12));
jLabel2.setText(“SISTEMA INTEGRADO DE ALARME”);jLabel3.setFont(new java.awt.Font(“Times New Roman”, 1, 14));
jLabel3.setForeground(new java.awt.Color(204, 0, 51));jPanel1.setBackground(new java.awt.Color(204, 204, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(null, new java.awt.Color(204, 255, 255)));
jButton1.setText(“Inc\u00eandio”);
jButton1.setToolTipText(“Esse bot\u00e3o dispara a mensagem de fogo para todas as O.M. do complexo”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Fogo(evt);
}
});jButton2.setText(“Invas\u00e3o”);
jButton2.setToolTipText(“Esse bot\u00e3o dispara a mensagem de assalto para todas as O.M. do complexo”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Assalto(evt);
}
});jButton3.setText(“Outra”);
jButton3.setToolTipText(“Esse bot\u00e3o habilita a op\u00e7\u00e3o de envio de mensagens para todas as O.M. do complexo”);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OutraMsg(evt);
}
});org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(26, 26, 26))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton3)
.add(jButton1)
.add(jButton2))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);jDesktopPane1.setBackground(new java.awt.Color(204, 204, 204));
jDesktopPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, “Insira seu texto aqui”, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(“Times New Roman”, 1, 12)));
jTextField1.setEditable(false);
jTextField1.setSelectedTextColor(new java.awt.Color(255, 255, 204));
jTextField1.setBounds(20, 20, 160, 30);
jDesktopPane1.add(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jTextField1.getAccessibleContext().setAccessibleParent(jButton3);jButton4.setText(“OK”);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EnviarMsg(evt);
}
});jButton4.setBounds(190, 20, 70, 30);
jDesktopPane1.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, “Mensagens Recebidas”, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(“Arial”, 1, 12), new java.awt.Color(0, 0, 0)));
jLabel4.setFont(new java.awt.Font(“Times New Roman”, 1, 12));
jLabel4.setForeground(new java.awt.Color(255, 0, 0));
jLabel4.setText(“t”);org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel2Layout.createSequentialGroup()
.add(28, 28, 28)
.add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(198, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
.add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(35, Short.MAX_VALUE))
);jMenu1.setText(“Arquivo”);
jMenuItem1.setText(“Sair”);
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Fechar(evt);
}
});jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText(“Sobre”);
jMenuBar1.add(jMenu2);setJMenuBar(jMenuBar1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 417, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(96, 96, 96)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jDesktopPane1)
.add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(layout.createSequentialGroup()
.add(226, 226, 226)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(179, 179, 179)
.add(jLabel2)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel2)
.add(33, 33, 33)
.add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(23, 23, 23)
.add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(36, 36, 36)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jDesktopPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(91, 91, 91))
);
pack();
}//
private void Fechar(java.awt.event.ActionEvent evt) {
// TODO adicione seu código de manipulação aqui:
System.exit(0);
}private void EnviarMsg(java.awt.event.ActionEvent evt) {
flag = true;
Cliente c = new Cliente();
List endereco = new ArrayList();
endereco = enderecos();
Iterator it = endereco.iterator();while (it.hasNext()){ String item = (String) it.next(); // c.Cliente(jTextField1.getText(),item)); }}
private void OutraMsg(java.awt.event.ActionEvent evt) {
jDesktopPane1.setVisible(true);
jTextField1.setEditable(true);
}private void Assalto(java.awt.event.ActionEvent evt) {
try{
flag = true;
Cliente c = new Cliente();
List endereco = new ArrayList();
endereco = enderecos();
Iterator it = endereco.iterator();
flag = true;
while (it.hasNext()){
String item = (String) it.next();
// c.Cliente(“Invasão”,item);
}
} catch (Exception e){
e.printStackTrace();
}
}private void Fogo(java.awt.event.ActionEvent evt) {
try{ flag = true; Cliente c = new Cliente(); List endereco = new ArrayList(); endereco = enderecos(); Iterator it = endereco.iterator(); flag = true; while (it.hasNext()){ String item = (String) it.next(); // c.Cliente("Incendio",item); } } catch (Exception e){ e.printStackTrace(); } //jLabel4.setText(retorno); -
// TODO adicione seu código de manipulação aqui:
}
/**
* @param args the command line arguments
*/
private static String pingar(String host) {
try {
if (InetAddress.getByName(host).isReachable(5000))//pinga na porta 5000
return("Respondendo: ");
else
return("Falha na conexão: ");
} catch (Exception e) {
return(“Erro de Sistema :”+ " - " + e);
}
}
// Lista os endereços do propriert
public static ArrayList enderecos(){
ArrayList endereco = new ArrayList();
Properties prop = new Properties();
InputStream in = SisAlarmeTela.class.getResourceAsStream("enderecos.properties");
try {
prop.load(in);
for (int i =1; i<= prop.size();i++){
endereco.add(prop.getProperty("tcp"+i));
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Erro");
}
return endereco;
}
public static ArrayList nome(){
ArrayList nome = new ArrayList();
Properties prop = new Properties();
InputStream in = SisAlarmeTela.class.getResourceAsStream("enderecos.properties");
try {
prop.load(in);
for (int i =1; i<= prop.size();i++){
nome.add(prop.getProperty("nome"+i));
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Erro");
}
return nome;
}
private static void respServer() {
try{
Cliente c = new Cliente();
c.Cliente(“Sem msg”);
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SisAlarmeTela().setVisible(true);
}
});
while(true){
//Lista os enderecos para o Ping
List endereco = new ArrayList();
List nome = new ArrayList();
endereco = enderecos();
nome = nome();
Iterator it = endereco.iterator();
Iterator itn = nome.iterator();
// PING EFETIVO
while (it.hasNext()& itn.hasNext()) {
String item = (String) it.next();
String name = (String)itn.next();
String tcp = pingar (item);
jLabel3.setText(tcp+" "+name);
}
//startando o servidor a partir daqui o programa simplesmente para
out = Servidor.StartServer(8000);
respServer();
jLabel3.setText(out);
}
}
// Declaração de variáveis - não modifique
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 static javax.swing.JLabel jLabel3;
private static javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField jTextField1;
// Fim da declaração de variáveis
private static String out;
private static int porta;
}