2017-03-01 4 views
-1

간단한 클라이언트 - 서버 GUI 응용 프로그램을 만드는 동안 사용자가 입력 한 도형 크기를 읽고 서버로 전송합니다 (drawShape이 호출 된 곳). 클라이언트가 (DIM x, y)를 받으면 클라이언트는 (DIM x, y)를받습니다. ou "1 : 그리기 점 \ n2 : 원 그리기 \ n3 .Double rectangular "그런 다음 출력하려고하는 내용이 서버에 전송되지 않습니다. (한 단어로 시도했지만) 작동하지 않았습니다.Java GUI 응용 프로그램 서버는 첫 번째 요청 만 읽습니다.

나는 정말로 무엇이 문제인지 모른다. 그리고 나는 며칠 동안 그것을 고민하고있다. 구문 분석 값, 닫힌 스캐너, 검사 된 스캐너, 검사 된 루프 ... PrintWriter가 OutputStream 응답을 서버에 보내지 않는 이유는 무엇입니까?

이 코드입니다 :

public static final int TCP_PORT = 8000; 

public SGPClientThread(Socket sock) throws IOException { 
    this.sock = sock; 

    in = new BufferedReader(new InputStreamReader(sock.getInputStream()),1); 
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true); 

    start(); 
} 

ETFCanvas can = new ETFCanvas(450, 500); 

public void run() { 

    Scanner scan = new Scanner(System.in); 

    System.out.println("Send new request by entering '<NEW>'"); 

    String option = ""; 
    option = scan.nextLine(); 
    out.println("CONNECT##" + option); 

    String read = " "; 
    try { 
     read = in.readLine(); 
    } catch (IOException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 
    if (read.startsWith("<DIM x,y>")) { 
     System.out.println("1:Draw point\n2:Draw circle\n3.Draw rectangle"); 
     // SO FAR SO GOOD! 

> 문제가 발생하는 경우, 아무것도 내가 서버로 전송되지 않습니다, 인쇄하려고, 그것은이 될 필요가 없습니다되는 코드에 따라, 단순히 작동하지 않는 단어 또는 간단한 것을 보내려고하면됩니다.

 **int choice = scan.nextInt(); 
     switch (choice) { 
     case 1: 
      System.out.println("Dimension and color of POINT:x1,y1,color"); 
      System.out.println("Enter X: "); 
      x = scan.nextInt(); 
      System.out.println("Enter Y: "); 
      y = scan.nextInt(); 
      do { 
       try { 
        System.out.println(
          "Enter Color value: ETFCanvas.COLOR_RED;ETFCanvas.COLOR_BLUE;ETFCanvas.COLOR_GREEN"); 
        color = scan.nextInt(); 
       } catch (InputMismatchException e) { 
        System.out.print("Invalid input "); 
       } 
       scan.nextLine(); // clears the buffer 
      } while (color <= 0); 
      scan.close(); 
      String iks = String.valueOf(x); 
      String ipsilon = String.valueOf(y); 
      String kolor = String.valueOf(color); 
      out.println("<POINT x,y,c>##" + iks + "##" + ipsilon + "##" + kolor); 
      break;** 

인용문

그리고 소켓이 짧은 나는 클라이언트의 나머지는 원형과 사각형 그리기에 딱 사례 2와 3 스레드 게시되지 않은 유지하고 폐쇄하기 .

다음은 내 서버 스레드 코드입니다.

ETFCanvas can = new ETFCanvas(450, 500); 

public ServerThread(Socket sock, int value) throws IOException { 

    this.sock = sock; 
    this.value = value; 

    // oos = new ObjectOutputStream(sock.getOutputStream()); 
    // ois = new ObjectInputStream(sock.getInputStream()); 
    in = new BufferedReader(new InputStreamReader(sock.getInputStream()),1); 
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true); 
    start(); 

} 

@Override 
public void run() { 

    String line = ""; 
    // 
    try { 
     line = in.readLine(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    // 
    if (line.startsWith("CONNECT##")) { 

     System.out.println("User sent request " + sock.getRemoteSocketAddress().toString() + line); 

     String[] content = line.split("##"); 
     req = content[1]; 
     if (req.equals("<NEW>")) { 
      out.println("<DIM x,y>"); 
     } 
    } else { 
     System.out.println("Bad request [" + sock.getRemoteSocketAddress().toString() + "]: " + line); 

>이 부분까지가 마법처럼 작동하고 당신이 읽을 서버 코드에서

} 
    if (line.startsWith("<POINT x,y,c>##")) { 

     System.out.println("User sent request TRY" + sock.getRemoteSocketAddress().toString() + line); 

     String[] dim = line.split("##"); 
     String dimX = dim[1]; 
     String dimY = dim[2]; 
     String dimC = dim[3]; 

     int x = Integer.parseInt(dimX); 
     int y = Integer.parseInt(dimY); 
     int c = Integer.parseInt(dimC); 

     can.drawPoint(x, y, ETFCanvas.COLOR_RED); 
    } else if (line.startsWith("<CIRCLE x,y,r,boja>##")) { 

     String[] dim = line.split("##"); 
     String dimX = dim[1]; 
     String dimY = dim[2]; 
     String dimR = dim[3]; 
     String dimC = dim[4]; 

     int x = Integer.parseInt(dimX); 
     int y = Integer.parseInt(dimY); 
     int r = Integer.parseInt(dimR); 
     int c = Integer.parseInt(dimC); 

     can.drawCircle(x, y, r, ETFCanvas.COLOR_RED); 
    } else if (line.startsWith("<RECTANGLE x,y,w,h,boja>##")) { 

     String[] dim = line.split("##"); 
     String dimX = dim[1]; 
     String dimY = dim[2]; 
     String dimW = dim[3]; 
     String dimH = dim[4]; 
     String dimC = dim[5]; 

     int x = Integer.parseInt(dimX); 
     int y = Integer.parseInt(dimY); 
     int w = Integer.parseInt(dimW); 
     int h = Integer.parseInt(dimH); 
     int c = Integer.parseInt(dimC); 
     can.drawRect(x, y, w, h, ETFCanvas.COLOR_RED); 
     ; 
     try { 
      in.close(); 
      out.close(); 
      sock.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 
} 

}

+1

공예 및 문제의 [mcve] 버전을 게시 고려하시기 바랍니다. –

답변

0

을 그리기 위해 고객에게 요청을 읽지 않습니다 입력에서 첫 번째 줄을 읽지 만 코드에서 다음 줄을 읽지 않습니다.

당신은 라인 프로세스를 읽고 반복 while 루프를 사용한다 :

boolean shouldProceed = true; 
while (shouldProceed) { 
    line = in.readLine(); 
    if (line.contains(...)) { 
     // do something 
    } else if (line.comtains(...)) { 
     // do something else 
    } else { 
     shouldProceed = false; 
    } 
} 
+0

예,이 문제가 해결되어 첫 번째 in.readLine 전에 while 루프를 넣어야했습니다. 이후에 배치했습니다. 감사합니다. –