将你接收数据的那点代码贴出来,另外存储数据的那个数组的定义也贴出来……

解决方案 »

  1.   

    这是我的服务器端Java程序,客户端通过CB向服务器发送信息,请大侠过目!不知为什么!!奇怪!
    public static void receivePacket(int port)
    {
      byte[] buff=new byte[9000];
      int l=0;
      DatagramPacket packet=new DatagramPacket(buff,buff.length);
      DatagramSocket socket=null;
      try{
        socket=new DatagramSocket(port);
       }catch(SocketException e){
          System.out.println("SocketException throws in creating\n");
          System.exit(1);
        }
       System.out.println("Server is working on port "+port);
       for(;;)
       {
           String msg="";
           try{
      socket.receive(packet);
    }catch(IOException e){
            System.out.println("IOException thrown on receive data!\n");
    System.exit(1);
    }
    try
    {
      l=packet.getLength();
      msg=new String(packet.getData(),0,l,"gb2312");
    }catch(Exception e){
      System.out.println("Data Convert Error!"); 
      continue;
    }
    System.out.println("Received Packet from "+packet.getAddress()+",port="+packet.getPort()+",len="+String.valueOf(l));
    System.out.println(msg);
    }
    }