客户端 - 服务器套接字字节传输

时间:2011-02-28 12:20:05

标签: android sockets tcp

我从客户端发送一个字节数组(android)

byte[] data = new byte[7];
    data[0] = "{".getBytes()[0];
    data[1] = "{".getBytes()[0];
    data[2] = (byte) 0xd1;
    data[3] = (byte) 0x01;
    data[4] = (byte) 0x00;//0xd1 + 0x00;
    data[5] = "}".getBytes()[0];
    data[6] = "}".getBytes()[0];
    os.write(data);  // os - Output stream

在服务器中我正在听它...

InputStream is = this.socket.getInputStream();
    DataOutputStream os = new DataOutputStream(this.socket.getOutputStream());

    // Set up input stream filters.
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    // Get the request line of the SMS request message.
    String requestLine = br.readLine();
    System.out.println(requestLine + "<---- Received message");

当我发送这样的数据时... br.readLine()没有被调用或卡住..当我从客户端发送一个字符串时

os.writeBytes(“自动消息”);它工作正常......

1 个答案:

答案 0 :(得分:1)

readLine()可能在它返回之前等待行终止符。您可以尝试以下方法:

byte[] data = new byte[8];
    data[0] = "{".getBytes()[0];
    data[1] = "{".getBytes()[0];
    data[2] = (byte) 0xd1;
    data[3] = (byte) 0x01;
    data[4] = (byte) 0x00;//0xd1 + 0x00;
    data[5] = "}".getBytes()[0];
    data[6] = "}".getBytes()[0];
    data[7] = "\r".getBytes()[0];
    os.write(data);  // os - Output stream