2011-08-29 1 views
1

나는 내 것과 비슷한 문제를 봤지만이 문제를 해결할 수 없었습니다. 릴레이 채팅을하려고합니다. 나는 플러싱을 다했다. 심지어 autoflush (println 사용)도 시도했습니다. 그러나 서버로 보낸 첫 번째 메시지가 끝나면 이후의 메시지는 더 이상 전송되지 않습니다. 나는 인쇄 작가를 닫지 않을 것이다. 소켓을 확인했는데 그래, 아직 연결되어있어 보낼 메시지를 인쇄했는데 아무것도 잘못된 것 같아. 도움말은 대단히 감사하겠습니다. 여기 PrintWriter가 첫 번째 메시지 만 보냄

클라이언트 코드의 일부이다 :

public void initializeConnection(){ 
    try { 
     host = InetAddress.getLocalHost(); 
     clientSocket = new Socket(host.getHostAddress(), port); 
     outToServer = new PrintWriter(clientSocket.getOutputStream(),true); 
     String message = outMsgArea.getText()+"Hello"; 
     outToServer.println(message); 
     System.out.println(clientSocket.isConnected()); 
    } 
    catch(IOException ioEx) { 
     ioEx.printStackTrace(); 
    } 
} 

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()==quit){ 
     try { 
      outToServer.close(); 
      clientSocket.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 
    else if(e.getSource()==button){ 
     if(outMsgArea.getText()!=null || !outMsgArea.getText().equals("")){ 
      /*try { 
       outToServer = new PrintWriter(clientSocket.getOutputStream(), true); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      }*/ 
      String message = outMsgArea.getText()+"Hello"; 
      System.out.println(clientSocket.isConnected()); 
      outToServer.println(message); 
      outToServer.flush(); 
      //outToServer.println(message); 
      outMsgArea.setText(""); 
     } 
    } 
} 

서버 :

 while(true) { 
      try { 
       Socket connectionSocket = servSocket.accept(); 
       Scanner inFromClient = new Scanner(connectionSocket.getInputStream()); 
       String clientSentence = inFromClient.nextLine(); 
       System.out.println(clientSentence); 
      } 
      catch(IOException ioEx) { 
       ioEx.printStackTrace(); 
      } 
     } 
    } 
+0

코드를 공유 할 수 있습니까? –

+0

몇 가지 코드를 게시하십시오. –

+0

서버를 작성 했습니까? 입력을 처리하는 코드를 게시 할 수 있습니까? – Kevin

답변

2

내가

Socket connectionSocket = servSocket.accept(); 
Scanner inFromClient = new Scanner(connectionSocket.getInputStream()); 

는 while 루프 내부에 있어야한다고 생각하지 않습니다.

+0

while 루프에 넣지 않으면 클라이언트에서 계속 메시지를 받아 들일 수 있습니까? @. 나는 어리 석다. – jayp

+0

나는 그것을 제거하려고 노력했다. 그리고 효과가있었습니다! 고맙습니다!!!!! – jayp

+0

그러면이 대답을 수락해야합니다. –