[英]Code not running after while loop in Java
我有最隨機的問題。 簡而言之,我希望在while循環中進行迭代,直到滿足某個條件為止,而當顯然不滿足該條件時,應運行循環后的代碼。 由於某些原因,while循環后的代碼無法運行...這是我的代碼:
while (true)
{
Socket ClientSocketConnection = serverSocket.accept();
System.err.println("We have established a connection with a client!");
System.out.println(" ");
ServerInput = new BufferedReader(new InputStreamReader(ClientSocketConnection.getInputStream()));
ServerOutput = new DataOutputStream(ClientSocketConnection.getOutputStream());
while((StringInput = ServerInput.read()) != -1)
{
//Things get done here
}
//methodBeingUsed is a string here
switch (methodBeingUsed){
case "GET":
GET();
break;
case "POST":
POST(sBuffer.toString());
break;
}
System.err.println("The Connection will now Terminated!");
ServerOutput.close();
ServerInput.close();
ClientSocketConnection.close();
}
}
基本上由於某些原因,switch語句未運行? 當我調試代碼時,我從ServerInput
變量獲取最后一個-1,代碼停止了。 在while(true)
循環中停止並且不繼續其他所有操作。 真的不確定為什么會這樣...
顯然,這是阻塞方法: ServerInput.read()
,這意味着它要等到實際讀取內容為止。 如果它什么也沒讀,那就在等待。 您可能未正確連接到現有連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.