日期:2014-05-20  浏览次数:20965 次

循环读取输入流的出现java.lang.NegativeArraySizeException怎么处理?
public class recv {
public static void main(String[] args) {
  recv atestprocesse=new recv();
  }
public recv() {
  ServerSocket serversocket=null;
  Socket so=null;
  OutputStream output=null;
  InputStream input=null;
  SGIP_Command command=null;
  try {
  serversocket=new ServerSocket(8809);
  System.out.println("等待接收");
  so=serversocket.accept();
  System.out.println("接收完毕");
  System.out.println("New connection accepted "
  + so.getInetAddress() + ":" + so.getPort());  
  command=new SGIP_Command();

  SGIP_Command tmpCMD=null;
  Deliver deliver=null;
  DeliverResp deliverresp = null;

  Report report=null;
  ReportResp reportresp=null;

  Userrpt userrpt = null;
  UserrptResp userrptresp = null;

  Bind active=null;
  Unbind term=null;
BindResp resp=null;
  UnbindResp Unresp=null;
  boolean loop=true;
  while(loop)
  {  
  input=new DataInputStream(so.getInputStream());
  output=new DataOutputStream(so.getOutputStream());  
  tmpCMD=command.read(input);//接收sgip消息
  System.out.println( tmpCMD.getTotalLength());
  switch (command.getCommandID())
  {  
  case SGIP_Command.ID_SGIP_BIND :
  {
  active=(Bind)tmpCMD;//强制转换  
  int result=active.readbody();//解包
  System.out.println(tmpCMD.getTotalLength());
  System.out.println(tmpCMD.getCommandID());
  System.out.println(tmpCMD.getSeqno_1());
  System.out.println(tmpCMD.getSeqno_2());
  System.out.println(tmpCMD.getSeqno_3());
  System.out.println(active.GetLoginType());
  System.out.println(active.GetLoginName());
  System.out.println(active.GetLoginPassword());
  resp=new BindResp(352101,//node id 3+CP_id
  0);//result
  resp.SetResult(1);
  resp.write(output);
  System.out.println("resp完成");
  break;
  }


  这是部分代码,测试的时候每次下发短息的回复报告都是出现
等待接收
接收完毕
New connection accepted /192.168.0.119:2973
61
61
1
3027352101
321161427
1
2
 resp完成
java.lang.NegativeArraySizeException
应该是数据流到最后-1标志结尾,但是程序还是读取导致的,我处理了,还是不行,不知道怎么办?


------解决方案--------------------
try块后面的代码没有贴出来。

其实这种问题无需问的,你找到报异常的行,Debug一下就行了。
------解决方案--------------------
探讨

serversocket=new ServerSocket(8809);
so=serversocket.accept();

input=new DataInputStream(so.getInputStream());
output=new DataOutputStream(so.getOutputStream())

input是读取的客户端的数据流,


tmpCMD=……