首先我使用的是 NetBeans IDE。有一个客户端,它的一个线程从服务器接收消息并将它们放入 Vector 中,另一个线程处理它们。MessageListener 和 MessageHandler 是那些。所以问题是它收到的第一条消息运行良好,但是对于下一条消息,当它调用方法 byte[] getFirstMessage() 时,它返回值为 0 的字节。
在我看来,问题出在 Vector addElement 方法上,它将第二条消息添加到索引 1 而不是 0,尽管它在将数据传递给 MessageHandler 或我使用时删除了 vector 的第一个元素的内容某些方法中的局部变量。PS它应该是消息队列。
消息监听器.java
package org.rebirth;
import java.io.*;
import java.util.*;
public class MessageListener implements Runnable{
Vector v;
int size = 0;
Connections con;
byte[] buffer = new byte[100000];
boolean noErrors = true;
public MessageListener(Connections con){
v = new Vector(50,10);
this.con = con;
Thread thr = new Thread(this);
thr.start();
}
public void run(){
while(noErrors){
try{
listenForData();
Thread.sleep(1);
}catch(Exception exc){
exc.printStackTrace();
noErrors = false;
}
}
}
public void listenForData() throws IOException{
con.fill(buffer,(byte)0);
System.out.println("Trying to receive data");
// InputStream
con.in.read(buffer);
System.out.println("Data received id "+con.ReadInt3Bytes(buffer,1));
v.addElement(buffer);
size++;
if(v.isEmpty()){
System.out.println("empty");
}
}
public byte[] getFirstMessage(){
if(v.size()>0){
byte[] data = (byte[]) v.firstElement();
v.removeElementAt(0);
size--;
System.out.println("first byte element "+(int)data[0]);
return data;
}
return null;
}
}
消息处理程序.java
package org.rebirth;
import java.util.*;
import javax.microedition.lcdui.game.*;
public class MessageHandler implements Runnable{
Vector v;
MessageListener lst;
Connections con;
int vienas = 1;
public MessageHandler(Vector v,MessageListener lst){
this.v = v;
this.lst = lst;
this.con = lst.con;
Thread thr = new Thread(this);
thr.start();
}
public void run(){
while(true){
try{
if(!v.isEmpty()){
handleMessages();
}
Thread.sleep(10);
}catch(Exception exc){}
}
}
public void handleMessages(){
// vectordsfds
int id;
byte [] gotByte = lst.getFirstMessage();
id=con.ReadInt3Bytes(gotByte,1);
System.out.println("handler id: "+id);
// call a method to handle received message;
handleMessage(id,gotByte);
}
public void handleMessage(int id,byte[] gotByte){
switch(id){
case 62:
// GameServerList
con.serverNumber = (int)gotByte[4];
System.out.println("Servers "+con.serverNumber);
int nri = 6;
for(int i=0;i<con.serverNumber;i++){
nameLength = (int)gotByte[nri];
nri+=1;
con.serverName[i] = new String(gotByte, nri, nameLength);
nri+=nameLength;
int ipLength = (int)gotByte[nri];
nri+=1;
con.serverIp[i] = new String(gotByte, nri, ipLength);
nri+=ipLength;
con.online[i] = con.ReadInt3Bytes(gotByte,nri);
nri+=3;
con.maxOnline[i] = con.ReadInt3Bytes(gotByte,nri);
System.out.println("Server name " +con.serverName[i]);
System.out.println("ip "+con.serverIp[i]);
System.out.println("online "+con.online[i]);
System.out.println("max online "+con.maxOnline[i]);
nri+=4;
}
break;
case 64:
//GameVersion
int success = (int)gotByte[4];
if(success == 1){
con.version=true;
System.out.println("Version match!");
}else{
System.out.println("version does not match");
System.out.println(success);
}
break;
}
}
}
编辑 2:我在读取数据之前添加了一个语句 InputStream 类 available() 方法。