java高手帮我看看这个问题:[toString() unavailable - no suspended threads]

cmathz1234 2010-03-31 01:59:11
我调试到Net这个类的socketComminication()这个函数的in.read(buffer, 0, 12);这一句的时候出了以上标题所写的错误!login()函数与socketComminication()函数的处理过程基本一致,为什么login()这个函数没问题socketComminication()这个函数却有问题?肯请大侠们帮我看看
一下是附的源代码
public class SocketPool {
private static int size = 10;
Vector<Socket> socketPool = new Vector<Socket>(size);

public SocketPool() {
ServerInfo[] infoArray = Ini.serArray;
if (infoArray == null) {

System.exit(-1);
return;
}

String hostname = new String(infoArray[0].getsServerAddr());
int port = infoArray[0].getnServerPort();

for(int count=0; count<10; ) {
try {
Socket socket = new Socket(hostname, port);
if(socket != null) {
count++;
socketPool.add(socket);
}
} catch (UnknownHostException e) {
continue;
} catch (IOException e) {
continue;
}
}
}

public synchronized Socket getSocket() {
//连接池为空,让线程等待
if (socketPool.isEmpty()) {
try {
wait();
} catch (InterruptedException e) {
}
}
return socketPool.remove(0);
}

public synchronized void closeSocket(Socket socket) {
socketPool.add(socket);
socket = null;
//连接为空,有加入一个socket时,通知其他线程有可用的线程
if (socketPool.size() == 1) {
notifyAll();
}
}
}


public class Net {
private static Logger log = Logger.getLogger(Jsds.JsdsName);

// 返回头
private int[] resHeader = new int[3];
// 返回参数
private byte[] resParams = null;

// 创建连接池
private static SocketPool pool = new SocketPool();

public int[] getResHeader() {
return resHeader;
}

public byte[] getResParams() {

* 取返回参数的给定长度字节
public byte[] getResParams(int offset, int len) {

/**
* 跟密码服务器握手
*
* @return
*/
private boolean login() {
ServerInfo[] infoArray = Ini.serArray;
if (infoArray == null) {
return false;
}
try {
Socket socket = pool.getSocket();
OutputStream out = socket.getOutputStream();
// log.log(Level.SEVERE, "out");
DataInputStream in = new DataInputStream(socket.getInputStream());
// log.log(Level.SEVERE, "in");

LoginRequestHeader lrh = new LoginRequestHeader();
lrh.setsConnectPwd(infoArray[0].getsConnectPwd());
byte[] buffer = lrh.toByte();
out.write(buffer);
out.flush();

// 解析返回头
buffer = new byte[12];
in.read(buffer, 0, 12);

pool.closeSocket(socket);
out = null;
in = null;
resHeader = ParseUtil.parseResponseHeader(buffer);
// 响应结果出错;
if (resHeader[2] != 0) {
log.log(Level.WARNING, "Return:%x"
+ Integer.toHexString(resHeader[2]));
return false;
}
} catch (UnknownHostException e1) {
log.log(Level.SEVERE, "UnknownHostException");
return false;
} catch (IOException e1) {
log.log(Level.SEVERE, "IOExceptio1n");
return false;
}
return true;
}

/**
* 与服务器通信
*
* @param commond
* 请求命令
* @param reqParams
* 请求参数
* @return
*/
public boolean socketComminication(int command, byte[] reqParams) {
log.log(Level.FINE, "Login");
boolean flag = login();
if (!flag) {
log.log(Level.SEVERE, "Login failed");
return false;
}
log.log(Level.FINE, "sendRequest");

byte[] buffer = null;

Request req = new Request(reqParams);
req.setnCommand(command);
buffer = req.toByte();
try {
Socket socket = pool.getSocket();
OutputStream out = socket.getOutputStream();
DataInputStream in = new DataInputStream(socket.getInputStream());

out.write(buffer);
out.flush();

错误: in.read(buffer, 0, 12);

resHeader = ParseUtil.parseResponseHeader(buffer);
if (resHeader[2] != 0) {
log.log(Level.WARNING, "Return:0x"
+ Integer.toHexString(resHeader[2]));
return false;
}
int resParamsLen = resHeader[0] - 12;
if (resParamsLen == 0) {
return true;
}
resParams = new byte[resParamsLen];
in.read(resParams, 0, resParamsLen);

pool.closeSocket(socket);
in = null;
out = null;
} catch (IOException e) {
log.log(Level.SEVERE, "IOException");
return false;
}

log.log(Level.FINE, "close");
// close();
log.log(Level.FINE, "Return");
return flag;
}
}
...全文
1093 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,619

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧