在製作一個簡單的客戶端 - 服務器GUI應用程序時,在讀取形狀的用戶輸入尺寸並將其發送回服務器時,在調用方法drawShape後,初始請求(「CONNECT ##」+ NEW)和服務器響應(DIM x,y),一切都停止,客戶端接收到(DIM x,y)打印ou 「1:繪製點\ n2:繪製圓圈\ n3 .Draw rectangle「和那麼它不會退回到服務器,無論我試圖輸出什麼,我都試圖輸出(我試過一個單詞),但它不起作用。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();
}
}
}
}
請考慮起草併發布你的問題的[MCVE]版本。 –