问题描述
我是java的真正新手,所以请原谅我这是一个毫无希望的直截了当的问题。
我的java游戏服务器中有以下内容:
//从客户端获取输入
DataInputStream in = new DataInputStream(server.getInputStream());
PrintStream out = new PrintStream(server.getOutputStream());
disconnect = false;
while((line = in.readLine()。trim())!= null&&!line.equals(。)&&!line.equals( )&&!disconnect){
System.out.println(Received+ line);
if(line.equals(h)){
out.println(h+ EOF); //客户端握手
System.out.println(Matched 1);
}否则if(line.equals(< policy-file-request />)){
out.println(...+ EOF); //策略文件
System.out.println(server.getInetAddress()+:Policy Request);
disconnect = true;
System.out.println(匹配2);
}否则if(line.substring(0,3).equals(GET)|| line.substring(0,4).equals(POST)){
out.println(HTTP / 1.0 200 OK \ nnServer:VirtuaRoom v0.9 \\\
Content-Type:text / html \ nnn ...); // HTML状态页
disconnect = true;
System.out.println(匹配3);
} else {
System.out.println(server.getInetAddress()+:未知命令,客户端断开连接。);
disconnect = true;
System.out.println(匹配其他);
}
}
server.close();
首先,客户端发送一个h数据包,并期望返回(握手) 。但是,我希望它在收到无法识别的数据包时断开客户端。出于某种原因,它对握手和HTML状态请求做出了很好的响应,但是当存在未知数据包时,从不执行else子句。
谢谢
根据评论中添加的信息,似乎将要发生的是客户端发送单个字符(例如'n')。该行
line.substring(0,3).equals(GET)|| line.substring(0,4 ).equals(POST))
将被执行,但是因为 line
只是一个字符 line.substring(0,3)
将抛出 StringIndexOutOfBoundsException
。要么是这导致你的程序失败而你没有提到它。或者你在代码的另一部分中进行了一些异常处理,这些异常处理是在你的代码的另一部分中没有显示出来的,这可能会导致错误或打印日志行或其他内容,而你又没有提到过(或注意到它)。
尝试用 startsWith
substring()。equals p>
I'm a real newbie to java, so please excuse me if this is a hopelessly straightforward problem.
I have the following from my java game server:
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
PrintStream out = new PrintStream(server.getOutputStream());
disconnect=false;
while((line = in.readLine().trim()) != null && !line.equals(".") && !line.equals("") && !disconnect) {
System.out.println("Received "+line);
if(line.equals("h")){
out.println("h"+EOF); // Client handshake
System.out.println("Matched 1");
}else if (line.equals("<policy-file-request/>")) {
out.println("..."+EOF); // Policy file
System.out.println(server.getInetAddress()+": Policy Request");
disconnect=true;
System.out.println("Matched 2");
}else if(line.substring(0,3).equals("GET")||line.substring(0,4).equals("POST")){
out.println("HTTP/1.0 200 OK\nServer: VirtuaRoom v0.9\nContent-Type: text/html\n\n..."); // HTML status page
disconnect=true;
System.out.println("Matched 3");
} else {
System.out.println(server.getInetAddress()+": Unknown command, client disconnected.");
disconnect=true;
System.out.println("Matched else");
}
}
server.close();
First of all, the client sends an "h" packet, and expects the same back (handshake). However, I want it to disconnect the client when an unrecognised packet is received. For some reason, it responds fine to the handshake and HTML status request, but the else clause is never executed when there's an unknown packet.
Thanks
From the information added in your comments it seems that what will be happening is the client is sending a single character (e.g. 'n'). The line
line.substring(0,3).equals("GET")||line.substring(0,4).equals("POST"))
will be executed, but since line
is only a single character line.substring(0,3)
will throw a StringIndexOutOfBoundsException
. Either this is causing your program to fail and you haven't mentioned that. Or you have some exception handling going on in another part of your code that you haven't shown and this is either supressing the error or printing a log line or something and again you haven't mentioned this (or noticed it).
Try replacing substring().equals
with startsWith
这篇关于Java if / else表现得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!