对于此应用程序,用户发出一条命令并且服务器发送一个回复。 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