2017-01-03 1 views
0

Java로 작성된 소켓 서버가 있습니다. 내 응용 프로그램은 기본적으로 소켓에서 데이터를 읽고 출력에 씁니다. 어쨌든 readUTF()으로 소켓에서 데이터를 읽으려고 할 때 주 스레드가 대기하게하고 두 번째 연결에서 EOFException이 발생하지만 을 사용하면 DataInputStream.readLine() 함수가 작동합니다.왜 DataInputStream.readUTF()가 주 스레드를 영원히 기다리게합니까? [소켓 프로그래밍]

readUTF()와 코드 :

private static PrintStream console = System.out; 

public static void main(String[] args) throws IOException{ 
    // TODO Auto-generated method stub 
    ServerSocket serverSocket = new ServerSocket(5112); 
    console.println("Server Started !"); 
    while(true){ 
     console.println("Loop"); 
     Socket client = serverSocket.accept(); 

     console.println("Processing Request"); 
     DataInputStream in = new DataInputStream(client.getInputStream()); 
     console.println("Proccessing in"); 
     console.println("Available : " + in.available()); 
     console.println("Request Body : \n"); 
     String str = in.readUTF(in); 
     console.println(str); 
     console.println("Waiting for new !"); 
    } 
} 

그리고 출력은 다음과 같습니다

Server Started ! Loop Processing Request Proccessing in Available : 0 Request Body :

답변

2

readUTF() 바이너리 인코딩 된 스트림, 텍스트가 아닌 데이터를 읽습니다. 따라서 코드는 인코딩으로 인해 수천 개의 문자를 예상합니다.

readUTF() 문서를 참조하십시오.

+0

답변 해 주셔서 감사합니다. –

관련 문제