2014-01-22 2 views
0

TCP를 사용하는 클라이언트를 수신하는 매우 간단한 TCP 서버를 수행하고 대문자로 메시지를 반환해야합니다.java 소켓 클라이언트가 서버로 전송하지만 수신 대기하지 않습니다.

연결이 ... 클라이언트가 완벽하게 메시지를 전송하고 서버가 그들에게 수신하지만, 서버가 응답하지 않습니다 나는이 일어나는 이유의 실마리를하지 않아도 잘 stablishes

서버 :

//imports and stuff you don't really care about 

public class ServerThread extends Thread { 

private final Socket clientSocket; 
private final Main controller; 
private final Server server; 

BufferedReader in; 
PrintWriter out; 

//constructor 
ServerThread(Socket clientSocket, Main controller, Server server) throws IOException { 
    this.clientSocket = clientSocket; 
    this.controller = controller; 
    this.server = server; 
    //make input and output streams 
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

    //THIS MAY BE WRONG 
    out = new PrintWriter(clientSocket.getOutputStream()); 
} 

@Override 
public void run() { 

    controller.out("Connected: " + clientSocket.getInetAddress().toString()); 
    out.println("test"); 
    try { 
     String msg; 
     while ((msg = in.readLine()) != null) { 
      //Prints this line 
      System.out.println(clientSocket.getInetAddress().toString() + " says: " + msg); 
      //THIS MAY BE WRONG 
      out.println(msg.toUpperCase()); 

      System.out.println("Answered");//this line too 
     } 
    }catch(SocketException ex){ 
     destroyMe(); 
    } 
    catch (IOException ex) { 
     Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex); 
     destroyMe(); 
    } 
} 

//couple of methods that don't interfere here 
} 

클라이언트 :

public class Client extends Thread { 

private final String host = "localhost"; 
private final int port = 44444; 
private final PrintWriter out; 
private final Socket socket; 
BufferedReader in; 

private Main c; 

public Client(Main c) throws IOException { 
    this.c = c; 

    socket = new Socket(host, port); 
    out = new PrintWriter(socket.getOutputStream(), true); 
    in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

    System.out.println("Connection Opened."); 
} 

public void send(String str) { 
    out.println(str); //this works fine 
} 

@Override 
public void run() { 
    String msg; 
    while (true) { 
     try { 
      //THIS MAY BE WRONG but I don't think so 
      while ((msg = in.readLine()) != null) { 
       System.out.println("received: " + msg); //this never happens 
       c.out(msg); 
      } 
      //this line is always reached until I close the connection. 
     } catch (SocketException ex) { 
      System.out.println("Connection closed"); //this line is reached too 
      break; 
     } catch (IOException ex) { 
      Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
      //This works fine 
     } 
    } 
}//end of the thread 

//there are a couple of methods here but they don't do anything related 
} 

나는 아무 잘못 표시되지 않지만 뭔가해야합니다.

도움 주셔서 감사합니다.

+0

당신이보고 할 수 있습니다 : 실제로 다른 스트림 옵션을 시도, http://stackoverflow.com/questions/2165006/simple-java-client-server-program –

+0

덕분에 로직 일이었다 do ... 나는 내가 너무 혼란 스러웠다 고 생각한다. –

답변

0

PrinterWriter를 사용하여 서버 코드의 출력을 제어하고 있습니다.

out = new PrintWriter(clientSocket.getOutputStream());을 통해 자동으로 비움을 켜지 않고 생성합니다. println, printf 또는 format 메서드를 통해 메시지를 쓸 때 문서에 따라 자동 비움이 발생합니다.

& 위의 방법을 사용하여 메시지를 작성하거나 메시지를 보낼 때 flush 메서드를 호출하거나 다른 메서드를 모두 사용하여 서버 출력 스트림을 제어 할 수 있습니다.

http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

+0

죄송 합니다만 투표하실 수는 없지만 감사드립니다.이 학교 업무의 종료일은 2 시간 만에 끝났고 저장했습니다. 정말 고마워 –

관련 문제