[英]Irregular output on second pass in Java
用戶選擇一個選項,在這種情況下為選項1-Brew。 然后,他們輸入URL,在本例中為BREW。 我現在僅檢查BREW一詞,因此現階段coffee:// 127 ...無關緊要。
從服務器收到消息響應。 請注意,在第一個服務器響應消息中發送了200 OK。
該應用程序繼續循環播放,用戶再次輸入URL,再次是BREW。
這次的問題是返回了400 Bad Request消息。 由於網址正確,因此他們應該獲得200 OK,就像第一次繞過應用程序一樣。
客戶端控制台:
WELCOME TO THE COFFEE POT APPLICATION!
Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW
MESSAGE FROM SERVER:
Message: BREW Accept-Additions: *, *, *, *, *, *, *,
HTCPCP-new 200 OK BREW START command completed.
Content-length: 44
Content-type: application/octet-stream
@@
Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW
MESSAGE FROM SERVER:
Message: BREW Accept-Additions: *, *, *, *, *, *, *,
HTCPCP-new 400 Bad Request.
Content-length: 5
Content-type: application/octet-stream
@@
Select an option:
1. Brew
2. Quit
客戶端線程處理程序:
import java.io.*;
import java.net.*;
import javax.activation.MimetypesFileTypeMap;
public class HTCPCPClientWorker extends Thread {
Socket cwsocket = null;
public HTCPCPClientWorker(Socket cwsocket) {
super("ClientWorker");
this.cwsocket = cwsocket;
}
@Override
public void run() {
String clientSentence = null;
BufferedReader inFromClient = null;
PrintWriter outToClient = null;
try {
inFromClient = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));
outToClient = new PrintWriter(cwsocket.getOutputStream(), true);
} catch (IOException ex) {
System.err.println("Cannot create streams");
}
try {
do { // end when client says QUIT
StringBuffer clientInputLine[] = new StringBuffer[3];
clientInputLine[0] = new StringBuffer();
clientInputLine[1] = new StringBuffer();
// Get next message from client
for (int i = 0; i <= clientInputLine.length; i++) {
// read input line from BufferedReader
clientSentence = inFromClient.readLine();
if (clientSentence.contains("BREW")) {
outToClient.println("Message: " + clientSentence);
outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
outToClient.println("Content-length: " + clientSentence.length()); // length needs to correspond to above line
outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
outToClient.println("@@");
outToClient.flush();
} else {
outToClient.println("Message: " + clientSentence);
outToClient.println("HTCPCP-new 400 Bad Request.");
outToClient.println("Content-length: " + clientSentence.length()); // length needs to correspond to above line
outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
outToClient.println("@@");
outToClient.flush();
}
// wait for EOF = @@
System.out.println("\tInput: " + clientSentence);
if (clientSentence.equals("@@") == true) {
break;
}
clientInputLine[i].append(clientSentence);
} // end for loop
} while (!clientSentence.contains("QUIT"));
outToClient.println("GOODBYE!");
outToClient.flush();
System.out.println("\tClient has disconnected.");
cwsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
} // end run
} // end HTCPCPClientWorker.java
我認為可以通過在每次通過時重新創建StringBuffer來解決此問題,但事實並非如此。
第三遍及以后似乎正常運行。
有什么想法嗎?
我相信您每次想從套接字讀取時都應該重新創建BufferedReader(inFromClient)。 使用readLine后,BufferedReader變空,因此,下次將輸入放到套接字上時,他將不會讀取它。
這里有更多關於讀寫套接字的信息 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.