對於此應用程序,用戶發出一條命令並且服務器發送一個回覆。 if
聲明由於某種原因而不起作用,並且對於其運行的每個輸入sentance = "Unknown Command" + '\n';
。我想知道我做錯了什麼。Java TCP if語句
import java.io.*;
import java.net.*;
// SERVER
class StartingPoint {
public static void main(String argv[]) throws Exception{
double srvversion = 0.1;
double cliversion = 0.1;
String clientSentence;
String sentance = null;
String capitalizedSentence;
//Commands cmds = new Commands();
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("Server Started");
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase();
if(capitalizedSentence == "VERSION"){
sentance = "Current Server Version: " + srvversion
+ " | Current Client Version: " + cliversion + '\n';
}else{
sentance = "Unknown Command" + '\n';
}
outToClient.writeBytes(sentance);
System.out.println("Sent: " + sentance);
}
}
}
你或許應該打印出什麼無效的命令_was_ - - 至少在試圖調試這個時候...... – sarnold 2011-12-29 05:43:12