套接字给出错误的响应

时间:2014-05-05 07:06:35

标签: java sockets byte endianness

以下是要传递给套接字以读取数据的参数:

Parameters : methodname(1 byte), payloadlength(2 byte), payload(2 byte)



public static void main(String[] args) {
    int methodname = 5;
    int payload  = 2151;
    int payloadLength = 2;

    ByteBuffer buffer = ByteBuffer.allocate(3 + payloadLength); // 3 = for method name + length
    buffer.order(ByteOrder.BIG_ENDIAN); // Just to be explicit

    buffer.put((byte) methodname);
    buffer.putShort((short) payloadLength);
    buffer.putShort((short) payload);

    buffer.rewind();
    byte[] result = new byte[buffer.capacity()]; // Could also use result = buffer.array();
    buffer.get(result);

            DataOutputStream classOUTstream = new DataOutputStream(socket.getOutputStream());// socket is already connected
                  classOUTstream.write(result);
                  classOUTstream.flush();

                  InputStream stream = socket.getInputStream();
                  byte[] data = new byte[100];
                  int count = stream.read(data);
        }

每次我算作 -1

任何人都可以帮助我从响应中获取正确的数据。

此致 阿卡什

0 个答案:

没有答案