2017-09-13 5 views
-1

최근에 java 소켓을 사용하여 PC의 java 서버 소켓에 연결할 수있는 응용 프로그램을 개발합니다. 이 코드는 스레드가 호출 된 직후 문자열을 보내는 서버에 연결할 때 완벽하게 작동하지만 서버를 닫고 시도 할 때 예외가 즉시 발생하지 않았지만 1 분 후에 유휴 상태 일 때만 예외가 발생합니다. 호출 된 페이지.소켓 던져 예외는 1 분 후에 만 ​​

다음

로그 캣 파일 서버가 열려 있고 또한 경우 가까운 그것 : 당신은 소켓 시간 초과 값을 지정해야합니다 https://imgur.com/a/lOqdq

public class checksession implements Runnable 
    { 
    private Activity activity; 
    Socket soc; 

    public checksession(Activity activity) 
    { 
     //get activity from class called 
     this.activity=activity; 
     //this.soc=soc; 

    } 


    @Override 
    public void run() 
    { 
     try{ 

      soc=new Socket("192.168.0.113",11000); 


      DataOutputStream dout=new DataOutputStream(soc.getOutputStream()); 


      //request format --> requestkey-field required 
      //format for LG request--> LG-username-password 
      String sendrequest="SS"; 

      //send requestcode to server 
      dout.writeUTF(sendrequest); 
      dout.flush();//refresh to make sure it send to the server 

      //wait for input 
      DataInputStream dis=new DataInputStream(soc.getInputStream()); 
      final String codefromserver=(String)dis.readUTF(); 
      System.out.println("reply:"+codefromserver); 


      String replycode[]= codefromserver.split("-"); 

      //server reply code format 
      // if not used on database RE-CK-NO 
      //if used on database RE-CK-YES 

      String sessionavailableornot=replycode[2]; 

      if(sessionavailableornot.equals("YES")) 
      { 
       activity.runOnUiThread(new Runnable() { 
        public void run() { 
         //Do your UI operations like dialog opening or Toast here 
         //navigate user to main screen 
         Intent in = new Intent(activity, sessiondetected.class); 
         in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         activity.startActivity(in); 
        } 
       }); 

      } 

      soc.close(); 

     }catch(Exception e) { 
      //runOnUiThread function is to let the function to be run on main thread 
      //bacause UI function cannot be run on worker thread 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        //Do your UI operations like dialog opening or Toast here 
        //navigate user back to connectionerror page so that user know 
        Intent inerr = new Intent(activity, serverconnectionerror.class); 
        inerr.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        activity.startActivity(inerr); 
       } 
      }); 
     } 

    } 
} 
+1

무엇을 'SO_TIMEOUT'으로 설정하면 어떻게 될까요? 'soc = new Socket ("192.168.0.113", 11000);의 직후에'soc.setSoTimeout (2000); –

+0

여기에 시간 제한을 설정하지 않았습니다. 따라서 플랫폼의 기본 시간 초과 (연결 시간 초과의 경우 약 1 분, 읽기 시간 초과의 경우 무한대)가 발생합니다. 네가 묻고있는 것이 불분명하다. – EJP

+0

@MickMnemonic 여전히 똑같은 타임 아웃을 설정했습니다. T_T –

답변

0

; 그렇지 않으면 플랫폼 기본값이 사용됩니다.

다음 코드는 소켓을 열기 전에 SO_TIMEOUT (소켓 읽기 제한 시간) 및 연결 시간 제한 값을 설정합니다.

final int timeout = 2000; // in millis 
soc = new Socket(); 
soc.setSoTimeout(timeout); 
soc.connect(new InetSocketAddress("192.168.0.113", 11000), timeout);