Java UDP数据包作为垃圾字符接收

时间:2016-09-04 08:42:17

标签: java udp

我编写了一个简单的UDP服务器程序,它接收来自网络控制器(Aruba)的数据包,但打印输出垃圾字符如下所示。尝试设置字符设置为UTF-8但没有帮助。

    public class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9999);
            byte[] receiveData = new byte[2048];
            byte[] sendData = new byte[2048];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String response = new String(receivePacket.getData(), 0, 
                          receivePacket.getLength(), "UTF-8");

                  System.out.println("RECEIVED: " + response);
               }
      }
}

输出:

RECEIVED: ��drÈ�Z��ï¿]о�,E!ï¿Uo�¿½ï¿½f�`�
RECEIVED: �(drÈ$w�D ��k�O���?�NQ��
pECEIVED: ��dr��.O�dLI�,O����u�
VT102RECEIVED: �Ddr��a�cd���k0��-���I�Q
RECEIVED: �dr���V���;;k��
$o���
ECEIVED: �^

0 个答案:

没有答案