2014-07-26 3 views
0

기본적으로 내 서버를 호출하고 페이지 소스 코드를 확인하는 프로그램을 작성했습니다. 유일한 문제는 무한 루프가 있기 때문에 서버의 소스 코드를 가져온 다음 루프의 맨 아래에 현재 페이지 코드를 저장하고 실제 페이지 코드를 다른 변수로 새로 고칩니다. 두 개의 변수가 일치하면 (아무 것도 변경되지 않았습니다.) 그런 다음 계속 반복하면서 페이지를 호출하고 소스 코드를 반복해서 가져옵니다. 이는 서버가 DoS 공격이라고 생각하고 잠시 후 연결을 허용하지 않을 경우를 제외하고는 훌륭하게 작동합니다. 나는 슈퍼 긴 루프가 될 방법을 만들려고했지만, 그 일을 지저분한 방법처럼 보인다. 내 질문은, 어쨌든 자바에게 단지 정해진 시간 동안 코드 실행을 완전히 멈추게하는 것이다. 예를 들어 서버를 1 ~ 2 분마다 호출합니다.코드 실행 중지 Java

package Mailer2; 
import java.util.Date; 
import java.util.Properties; 
import java.util.Scanner; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

import javax.mail.Authenticator; 
import javax.mail.Message.RecipientType; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
public class mailer2 { 
    static Scanner console = new Scanner(System.in); 
    public static void main(String[] args) throws AddressException, MessagingException, IOException, InterruptedException{ 
     System.out.println("Admin console"); 
     boolean StartServer = true; 
     String old = ""; 

     while(StartServer == true){ 
      Thread.sleep(10000); 
     // Get Info from Page 
     String From = ""; 
     String To = ""; 
     String Subject = ""; 
     String Body = ""; 
     String Provider = ""; 
     URL GetInfo = new URL("http://smsmessenger.comuf.com/Users.php"); 

     String GetPage = getPageCode(GetInfo); 
     //System.out.println("Got the String:" + GetPage); 
     String CutDown = GetPage.substring(73,GetPage.indexOf("<br")); 
     //System.out.println(CutDown); 
     From = CutDown.substring(0,CutDown.indexOf("TO:")); // From 
     //System.out.println(From); 
     To = CutDown.substring(CutDown.indexOf("TO:") + 3,CutDown.indexOf("SUBJECT") - 1); 
     //System.out.println(To); 
     Subject = CutDown.substring(CutDown.indexOf("SUBJECT:") + 8,CutDown.indexOf("BODY")); 
     //System.out.println(Subject); 
     Body = CutDown.substring(CutDown.indexOf("BODY:") + 5, CutDown.indexOf("Provider")); 
     //System.out.println(Body); 
     Provider = CutDown.substring(CutDown.indexOf("Provider:") + 9); 
     //System.out.println(Provider); 

     // Cell provider check 
     if(Provider.length() == 0){ 
      System.out.println("No Provider Detected!"); 
     } 
     else if(Provider.toLowerCase().equals("sprint")){ 
      To += "@messaging.sprintpcs.com"; 
      //System.out.println("Sprint HERE:" + To); 
     } 
     else if(Provider.toLowerCase().equals("verizon")){ 
      To += "@vtext.com"; 
     } 
     else if(Provider.toLowerCase().equals("att")){ 
      To += "@txt.att.net"; 
     } 
     else if(Provider.toLowerCase().equals("tmobile") || Provider.toLowerCase().equals("t-mobile")){ 
      To += "@tmomail.net"; 
     } 
     else{ 
     System.out.println("Provider NOT FOUND" + Provider); 
     } 
     if(!old.equals(GetPage)){ 
      System.out.println("SENDING EMAIL!!"); 
     SendEmail(From,To,Subject,Body); 
     } 
     System.out.println("Sleeping"); 
     Thread.sleep(100); 
     old = GetPage; 

     } 


    } 

    public static void SendEmail(String From, String To, String Subject, String Body) throws AddressException, MessagingException{ 

     Properties prop = new Properties(); 
     prop.put("mail.smtp.host", "ssrs.reachmail.net"); 
     prop.put("mail.smtp.port", "587"); 
     prop.put("mail.smtp.auth", "true"); 
     prop.put("mail.smtp.starttls.enable", "true"); 


     Session session = Session.getDefaultInstance(prop, new Authenticator() { 

      // Override method to Authenticate to mail server 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("MCA28\\admin", "pass"); 
      } 
      }); 

     session.setDebug(true); 

     MimeMessage Msg = new MimeMessage(session); 
     //System.out.println("Please enter the From:"); 
     //String setFrom = console.nextLine(); 
     Msg.setFrom(new InternetAddress(From)); 
     //System.out.println("Please enter which email to send to"); 
     //String setTo = console.nextLine(); 
     Msg.setRecipients(RecipientType.TO, To); 
     //System.out.println("Set the Subject"); 
     //String subject = console.nextLine(); 
     Msg.setSubject(Subject); 
     //System.out.println("Set the message Body"); 
     //String MessageBody = console.nextLine(); 
     // Initiate MimeBodyPart for filling email content 
     MimeBodyPart messagePart = new MimeBodyPart(); 
     messagePart.setText(Body); 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messagePart); 
     Msg.setContent(multipart); 

     // Email Sending process 
     Transport.send(Msg); 

    } 
    public static String getPageCode(URL SQL) throws IOException // Gets URL, returns InputLine 
    { 
     URLConnection openLine = SQL.openConnection(); 
     BufferedReader in = null; 
     String inputLine = null; 
     String line; 
     in = new BufferedReader(new InputStreamReader(openLine.getInputStream())); 

     while((line = in.readLine()) != null) 
     { 
      inputLine += line; 
     } 

     return inputLine; 

    } 


} 
+0

postt 일부 코드. –

+0

아마도 타이머를 사용 하시겠습니까? – user2864740

+0

어, 코드에 이미'Thread.sleep (10000);과'Thread.sleep (100);'이 있습니다. 이것들은 어떻게 생각하십니까? –

답변

0

변경 사항이없는 경우 다음 재 쿼리까지 시간을 두 배로하십시오. 몇 가지 미리 설정된 최대 값 (투표 사이의 시간)이 될 때까지 두 배로 유지하십시오. 변경 사항을 감지하면 더 자주 쿼리로 돌아갈 수 있습니다.