2012-02-29 2 views
1

업데이트 -의 DataInputStream

클라이언트 일관된 유형이 제공 솔루션으로 이동이 서버 소켓에 메시지를 전송, 서버는 원래의 메시지와 함께 클라이언트에 응답합니다. 후자의 기능을 도입 할 때, 서버는 상기 메시지를 계속 수신하기보다는 하나의 메시지만을 수신하고, 클라이언트에 응답하지 않는다. 추가 된 행에 주석 처리됨. 전화 끊기에 대한 통찰력은 대단 할 것입니다.

클라이언트 측 주석 문제와 코드 업데이트 응답에서 :

{ private Socket socket    = null; 
    private BufferedReader console = null; 
    private DataInputStream streamIn = null; 
    private DataOutputStream streamOut = null; 

while (!line.equals(".bye")) 
    { try 
    { line = console.readLine(); 
     streamOut.writeUTF(line); //Send console data to server socket 
     String reply = streamIn.readUTF(); //Recieve confirmation msg from server 
     System.out.println(reply); //Print the msg 
     streamOut.flush(); 
    } 

public void start() throws IOException 
    { console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader 
     streamIn = new DataInputStream(socket.getInputStream()); 
     streamOut = new DataOutputStream(socket.getOutputStream()); 
    } 
    public void stop() 
    { try 
     { if (console != null) console.close(); 
     if (streamOut != null) streamOut.close(); 
     if (streamIn != null) streamIn.close(); //Is it good practice to close 
     if (socket != null) socket.close(); 
     } 

서버 측 문제가 댓글을 달았습니다. 당신은 된 writeUTF 및 readUTF는하지만 난 그게 새로운 라인을 기다립니다으로 차단하는 기대 한 곳의 streamIn.readLine()에서를 사용하는

public void handleClient() throws IOException { 
     boolean done = false; 
     try { 
     System.out.println("Server Thread " + ID + " running."); 
     while (!done) { 
     String nextCommand = streamIn.readUTF(); 
     if(nextCommand.equals(".bye")) { 
      System.out.println("Client disconnected with bye."); 
      done = true; 
     } else { 
      System.out.println(nextCommand); 
      String nextReply = "\"You sent me ->" +nextCommand.toUpperCase() + "\"\n"; 
      streamOut.writeUTF(nextReply); 
     } 
    } 
    } finally { 
    streamIn.close(); 
    streamOut.close(); 
    socket.close(); 
    } 
    } 
    public void open() throws IOException 
    { 
     streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
     streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); 
//  streamOut = new DataOutputStream(socket.getOutputStream()); 
    } 
    public void close() throws IOException 
    { if (socket != null) socket.close(); 
     if (streamIn != null) streamIn.close(); 
     if (streamOut != null) streamOut.close(); 
    } 
+0

이 파일이 컴파일됩니까? 그렇지 않으면 streamOut.writeBytes (nextReply.getBytes()); ? – Zaki

+0

@ Zaki 코드가 컴파일됩니다. – Astron

답변

1

. readUTF를 일관되게 사용해야한다고 생각합니다.

BTW 콘솔은 데이터 스트림이 아니며 텍스트이기 때문에 BufferedReader를 사용하는 것이 좋습니다.

+0

저는 UTF에서'readLine()'과'writeBytes()'로 옮겼습니다. 적절하게 버퍼링 되었으면합니다. 서버가 더 이상 클라이언트로부터 데이터를 수신하지 않으므로 * UTF()를 사용해야합니까? – Astron

+0

readLine()이 개행을 기다리므로 writeBytes() 끝에서 새 행을 보내는 것으로 가정합니다. 그렇지 않으면, 내가 제안한 것처럼 writeUTF/readUTF를 사용할 것이다. –

+0

읽기/쓰기로 되돌아갑니다 .UTF, 이전과 비슷한 증상으로 클라이언트는 초기 메시지를 보내고 이후에는 더 이상 메시지를받지 못합니다. – Astron

관련 문제