请教:利用socket发送自定义数据包,应该用哪个类来存放自定义的数据啊
我要传送3个数据,2个int类型,一个string类型,我该用什么类来定义传送的数据包?用的是tcp协议。
或者换成这个问题,怎样将int、float等转换成byte[]类型?我需要取到int的每个个字节

解决方案 »

  1.   

    //为server端传送节点信息
    MyClass clientid = new MyClass ();
    clientid.setint1(int1);
    clientid.setint2(int2);
    clientid.setString(str);ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream()); 
                  
              out.writeObject(clientid);
      

  2.   

    int转byte
    int n = 1;
    byte[] bytes = new byte[4];
    byte[0] = (byte)n & 0x0F;
    byte[1] = (byte)((n >> 8) & 0x0F);
    byte[2] = (byte)((n >> 16) & 0x0F);
    byte[3] = (byte)((n >> 24) & 0x0F);float转byte
    float f = 0.0f;
    int n = Float.floatToIntBits(f);
    然后转换n为字节数组