Obtendo e verificando IP´s via arquivo de properties

7 respostas
E

Ola pessaol.

Tenho uma classe java que lê um arquivo .properties. Neste arquivo estão relacionados vários IP´s onde obtenho IP por IP e passo uma porta (fixa) para um socket que verifica se o IP/serviço esta ok, senão ele lança uma exception (Connection refused) etc.

O problema é o seguinte, se todos os IP´s/serviços estiverem ok rodando/no ar, blz ele me relaciona todos dizendo ok, se 1 único estiver fora, ele lança uma exception e quebra meu while (Código abaixo).

Alguém pode me dar sugestões de como resolver isso? Pensei um pouco e nada até agora.


import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.net.Socket;

public class Tester {

public static void main(String[] args){

	int Porta = 12345;
	String IPs = null;
	Socket socket = null;
	File file = null;	
	FileReader fileReader = null;
	BufferedReader bufferedReader = null;
	String quebra = "\n";
	String traco = "-----------------------------------------------------------------------" + quebra;
	String IPs_R_OK = traco + "Servico no(s) servidor(es) abaixo: ** ATIVO - OK! **" + quebra + quebra;
	String IPs_R_ERRO = traco + "Servico  no(s) servidor(es) abaixo: ## INATIVO - ERRO! ##" + quebra + quebra;
	String IPs_OK = null;		//Acumula ip´s ok´s lidos-verificados
	String IPs_ERRO = null;		//Acumula ip´s erro´s lidos-verificados
	String plataforma = "*** ATENCAO ***\n ERRO. Servico provavelmente fora de Atividade!!!";
	try {
		file = new File("c:/IPs/IPs.properties");
		fileReader = new FileReader(file);
		bufferedReader = new BufferedReader(fileReader);

		//Se o arquivo não estiver fazio leio todos os ip´s passando para o socket
		if(file.length() > 0){
			while((IPs = bufferedReader.readLine()) != null ){
				
				//Como resolver aquie se der pau?
				socket = new Socket(IPs, Porta);

				//Serviços OK
					IPs_OK += IPs + quebra;
                                                                            //Serviços EROO
                                                                                            IPs_ERRO += IPs + quebra;
				}
			}
			socket.close();
	} catch (FileNotFoundException ex) {
		System.out.println("*** ATENCAO: Arquivo " + file + " inexistente ou vazio. ***");
	} catch(IOException ioE){
		System.out.println("*** Problemas durante a conexao utilizando socket. ***");
		ioE.printStackTrace();
	}finally {
		if(IPs_OK != null){
			System.out.println(IPs_R_OK + IPs_OK + traco);
		}
		if (IPs_ERRO != null){
			System.out.println(IPs_R_ERRO + IPs_ERRO + traco + plataforma);
		}
		if (socket != null) {
			try {
				socket.close();
			}catch (IOException ioe) {
				ioe.printStackTrace();
				System.out.println("*** Erro ao fechar conexao socket. ***");
			}// Fim if close socket

		}// Fim último laço

	}// Fim finally

}// Fim metodo main

}//Fim da classe Tester


Agradeço s força de todos.
Vlw.

7 Respostas

J

Só você tratar a exceção:

try{ socket = new Socket(IPs, Porta); socket.close(); IPs_OK += IPs + quebra; } catch (IOException ioe) { IPs_ERRO += IPs + quebra; }

E

Olá Jair.

Satisfação uma resposta sua. Vlw a força.

Então, eu já havia tratado as exceptions, porém observei que quando da a primeira ele relaciona o IP dizendo que realmente ta fora, mas pará e não executa/continua o while. Como o arquivo contém vários IP´s a verificação é interrompida na primeira exception.

Minha lógica ta errada, devo fazer algo após tratar/capturar a exceptions para retornar ao while etc…?

Agradeço.

Vlw

J

Como ficou seu codigo?

E
try {

file = new File("c:/IPs/IPs.properties");

fileReader = new FileReader(file);

bufferedReader = new BufferedReader(fileReader);

if(file.length() > 0){

while((IPs = bufferedReader.readLine()) != null ){

socket = new Socket(IPs, Porta);

socket.close();

//Serviços OK

IPs_OK += IPs + quebra;

}

}

} catch (FileNotFoundException ex) {

System.out.println("*** ATENCAO: Arquivo " + file + " inexistente ou vazio. ***");

} catch(IOException ioE){
IPs_ERRO += IPs + quebra;

}finally {

if(IPs_OK != null){

System.out.println(IPs_R_OK + IPs_OK + traco);

}

if (IPs_ERRO != null){

System.out.println(IPs_R_ERRO + IPs_ERRO + traco + plataforma);

}

if (socket != null) {

try {

socket.close();

}catch (IOException ioe) {

ioe.printStackTrace();

System.out.println("*** Erro ao fechar conexao socket. ***");

}// Fim if close socket
J

O try catch deve ficar dentro do while.

try { file = new File("c:/IPs/IPs.properties"); fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); if(file.length() > 0){ while((IPs = bufferedReader.readLine()) != null ){ try{ socket = new Socket(IPs, Porta); //Serviços OK IPs_OK += IPs + quebra; } catch (IOException ioe) { IPs_ERRO += IPs + quebra; } finally { socket.close(); } } } } catch (FileNotFoundException ex) { System.out.println("*** ATENCAO: Arquivo " + file + " inexistente ou vazio. ***"); } finally { if(IPs_OK != null){ System.out.println(IPs_R_OK + IPs_OK + traco); } if (IPs_ERRO != null){ System.out.println(IPs_R_ERRO + IPs_ERRO + traco + plataforma); } }

E
try { 
    file = new File("c:/IPs/IPs.properties"); 
    fileReader = new FileReader(file); 
    bufferedReader = new BufferedReader(fileReader); 
    if(file.length() > 0){ 
       while((IPs = bufferedReader.readLine()) != null ){ 
                 socket = new Socket(IPs, Porta); 
                 socket.close(); 
                //Serviços OK 
                IPs_OK += IPs + quebra; 
        } 
    } 
} catch (FileNotFoundException ex) { 
    System.out.println("*** ATENCAO: Arquivo " + file + " inexistente ou vazio. ***"); 

} catch(IOException ioE){ 
    IPs_ERRO += IPs + quebra; 

}finally { 
    if(IPs_OK != null){ 
    System.out.println(IPs_R_OK + IPs_OK + traco); 
} 
if (IPs_ERRO != null){ 
    System.out.println(IPs_R_ERRO + IPs_ERRO + traco + plataforma); 
} 
if (socket != null) { 
    try { 
    socket.close(); 
}catch (IOException ioe) { 
    ioe.printStackTrace(); 
    System.out.println("*** Erro ao fechar conexao socket. ***"); 

}// Fim if close socket
E

Ok Elton Show.

Agora ficou blz.

Agradeço muitissimo sua força.

Abs e vlw.

try {
			file = new File("c:/IPs/IPs.properties");
			fileReader = new FileReader(file);
			bufferedReader = new BufferedReader(fileReader);
			if(file.length() > 0){
				while((IPs = bufferedReader.readLine()) != null ){
					try{
						socket = new Socket(IPs, Porta);
						socket.close();
						//Serviços OK
						IPs_OK += IPs + quebra;
					}catch(IOException ioE){
						//Serviços ERRO
						IPs_ERRO += IPs + quebra;
					}
				}
			}
		} catch (FileNotFoundException ex) {
			System.out.println("*** ATENCAO: Arquivo " + file + " inexistente ou vazio. ***");
		}finally {
			if(IPs_OK != null){
				System.out.println(IPs_R_OK + IPs_OK + traco);
			}
			if (IPs_ERRO != null){
				System.out.println(IPs_R_ERRO + IPs_ERRO + traco + plataforma);
			}
			if(socket != null){
				try{
					socket.close();
				}catch(IOException ioE){
					System.out.println("* Problema ao fechar conexao socket.*");
					ioE.printStackTrace();
				}
			}

		}// Fim finally

	}// Fim metodo main

}//Fim da classe Tester
Criado 3 de abril de 2008
Ultima resposta 3 de abr. de 2008
Respostas 7
Participantes 2