2011-08-15 6 views
1

이것은 Java의 단순 TCP 서버 (에코) 및 TCP 클라이언트입니다.첫 번째 SocketTimeoutException 후 EOFException 수신

st.setSoTimeout(WAITING_FOR_INPUT); 

을하지만이 소켓의 시간 제한 방법을 사용하는 경우, 나는 (여전히 좋은) 처음있는 InterruptedIOException을 얻을 것이다 :

내가 다음 줄을 주석 처리하면 프로그램이 작동된다. 그러나 다음 반복에서는 EOFException이 발생합니다 (이것이 문제입니다).

내가 이해가 안되는데, 무엇이 문제입니까? 도와주세요!

CSimpleServer.java

import java.io.*; 
import java.net.*; 

public class CSimpleServer 
{ 
    public static final int WAITING_FOR_INPUT = 10; // 10 ms 

    public CServerThread st; 

    public class CServerThread extends Thread 
    { 
     ServerSocket ss; 
     Socket st; 
     ObjectInputStream in; 
     ObjectOutputStream out; 

     int sleeping_time; 
     int port; 

     CServerThread(int port, int sleeping_time) 
     { 
      st = null; 
      in = null; 
      out = null; 

      this.port = port; 
      this.sleeping_time = sleeping_time; 
     } 

     public void run() 
     { 
      try { 
       ss = new ServerSocket(port); 
      } 
      catch (IOException ioe) { 
       System.err.println("Create server failed: " + ioe.getMessage()); 
       return; 
      } 

      try { 
       st = ss.accept(); 
       st.setSoTimeout(WAITING_FOR_INPUT); 
       out = new ObjectOutputStream(st.getOutputStream()); 
       out.flush(); 
       in = new ObjectInputStream(st.getInputStream()); 
      } 
      catch (IOException ioe) { 
       System.err.println("Catch connection failed: " + ioe.getMessage()); 
       return; 
      } 

      while (true) { 
       try { 
        int i_data = in.readInt(); 
        if (i_data == 0) { 
         break; 
        } 
        System.out.println("Get: " + i_data); 
        out.writeInt(i_data); 
        out.flush(); 
        out.reset(); 

        sleep(sleeping_time); 
       } 
       catch (InterruptedIOException ire) { 
        System.out.println("Read data timeout."); // for debug 
        continue; 
       } 
       catch (EOFException eof) { 
        System.err.println("Reach end of stream: " + eof.getMessage()); 
        break; 
       }    
       catch (IOException ioe) { 
        System.err.println("Read data failed: " + ioe.getMessage()); 
        break; 
       } 
       catch (InterruptedException ire) { 
        System.err.println("Something interrupted this thread: " + ire.getMessage()); 
       } 
      } 

      try { 
       in.close(); 
       out.close(); 
       st.close(); 
       ss.close(); 
      } 
      catch (IOException ioe) { 
       System.err.println("Close server failed: " + ioe.getMessage()); 
       return; 
      } 
     } 
    } 

    CSimpleServer() 
    { 
     st = null; 
    } 

    public static void main(String[] args) 
    { 
     CSimpleServer prog = new CSimpleServer(); 

     prog.st = prog.new CServerThread(3800, 1000); 
     prog.st.start(); 
    } 
} 

CSimpleClient.java

import java.io.*; 
import java.net.*; 

public class CSimpleClient 
{ 

    public CClientThread ct; 

    public class CClientThread extends Thread 
    { 
     Socket st; 
     ObjectInputStream in; 
     ObjectOutputStream out; 

     int port; 

     CClientThread(int port) 
     { 
      st = null; 
      in = null; 
      out = null; 

      this.port = port; 
     } 

     public void run() 
     { 
      try { 
       st = new Socket(InetAddress.getLocalHost(), port); 
       out = new ObjectOutputStream(st.getOutputStream()); 
       out.flush(); 
       in = new ObjectInputStream(st.getInputStream()); 
      } 
      catch (UnknownHostException uhe) { 
       System.err.println("Unknown server: " + uhe.getMessage()); 
       return; 
      }   
      catch (IOException ioe) { 
       System.err.println("Connect to server failed: " + ioe.getMessage()); 
       return; 
      } 

      BufferedReader ink = new BufferedReader(new InputStreamReader(System.in)); 

      while (true) { 
       try { 

        String s = ink.readLine(); 
        int i_data = Integer.valueOf(s); 
        out.writeInt(i_data); 
        out.flush(); 
        out.reset(); 
        if (i_data == 0) { 
         ink.close(); 
         break; 
        } 
        i_data = in.readInt(); 
        System.out.println("Echo: " + i_data); 
       } 
       catch (IOException ioe) { 
        System.err.println("Send/read data failed: " + ioe.getMessage()); 
        break; 
       } 
      } 

      try { 
       in.close(); 
       out.close(); 
       st.close(); 
      } 
      catch (IOException ioe) { 
       System.err.println("Close client failed: " + ioe.getMessage()); 
       return; 
      } 
     } 
    } 

    CSimpleClient() 
    { 
     ct = null; 
    } 

    public static void main(String[] args) 
    { 
     CSimpleClient prog = new CSimpleClient(); 

     prog.ct = prog.new CClientThread(3800); 
     prog.ct.start(); 
    } 
} 

답변

0

나요 당신은 10 MS는 상황에 따라 매우 빠른 네트워크입니다, 시간 제한 값을 증가하려고합니다. ..

나는 그 이름에 상수 단위를주고 싶다. 코드는 더 많다. 귀엽다.

감사합니다, 당신이 피어가 연결, 그것에 대해 아무 두 가지 방법을 폐쇄 의미 EOFException을 가지고있는 경우 스테판

0

.

나는 Snicolas에 동의합니다. 10ms는 터무니없이 짧은 시간 제한입니다. 최소 5 초로 설정하십시오.

ObjectInputStream을 생성 한 후 후에도 시간 제한을 으로 설정해야합니다.