我正在编写一个解析操作码的服务器(只是一个简单的小爱好MMO服务器来教我自己的绳索;此刻它有点乱)我无法让服务器解析第二个操作码。我不确定是什么问题。每个操作码的格式是一个8字节的头,比如PRINTMSG,然后是主体,在这种情况下可以是诸如Hello之类的消息。每个操作码都以“|”结尾。我的代码似乎正在正确地获取和解析每个操作码,但是它没有执行任何超过第一个操作码的操作,这使我相信有一些我对BufferedReaders没有理解的东西。我正在使用read()读取它,并将每个字符存储在48字节的数组中。 Autoflushing设置为true。
以下是相关的服务器代码:
public String readInputStream() {
String msg = "";
char[] charArray = new char[48];
short i = 0;
char current = '#';
int characterInt = -1;
while (current != '|') {
try {
if (in.ready()) {
characterInt = in.read();
if (characterInt == (-1)) continue;
current = (char) characterInt;
if (current == '|') break;
charArray[i] = current;
//if ((charArray[i] == 'n') && (charArray[i-1] == '\\')) {
// charArray = new char[48];
//}
i++;
}
}
catch (IOException e) {
System.out.println("Error reading input stream in server ConnectionThread");
if (DEBUGMODE) System.exit(1);
}
}
msg = new String(charArray);
msg = msg.substring(0,i);
return msg;
}
public static void printMsg(String msg) {
System.out.println("(Op-PRINTMSG): " +msg+"\n");
}
public static void executeOpCode(String op, String body) {
if (op.equals("ABSPLAYX")) receivedPlayerAbsoluteX(body);
else if (op.equals("ABSPLAYY")) receivedPlayerAbsoluteY(body);
else if (op.equals("FIRSTVOL")) requestVolley();
else if (op.equals("SECNDVOL")) returnVolley(body);
else if (op.equals("PRINTMSG")) printMsg(body);
}
主循环:
public void run() {
while (shouldBeListening) {
//Main loop for the connection thread
String op = readInputStream();
//If stream has data, add the opcodes to opQueue.
if (op != ("")) {
separateOpsFromStream(op, incomingOpQueue);
}
//Extract incoming opcode head and body, then execute.
//This executes everything in the queue before moving on.
while (incomingOpQueue.size() > 0) {
op = incomingOpQueue.poll();
if (op != "") {
String opHeader = OpCodeOperations.readHeader(op);
String opBody = OpCodeOperations.readBody(op);
playerData.executeOpCode(opHeader, opBody);
}
}
}
}
答案 0 :(得分:0)
您是否尝试过标记并重置阅读器?
bufferedReader.mark(0);
bufferedReader.reset();
答案 1 :(得分:0)
好吧,我没有完全理解你的问题,但这是我的建议。您是否尝试修改邮件?每条消息都必须以" / n"或EOL标记结束。可能是那个吗?
答案 2 :(得分:-1)
变化:
if (op != "")
为:
if (!op.isEmpty())