简体   繁体   English

C服务器到Java客户端套接字读取以短值传递

[英]C server to Java client socket read passed in short value

I am having difficulty just passing a short value from a C server. 我很难从C服务器传递一个简短的值。 What I receive on the other end is 'junk' it looks like, and I can't seem to convert it into something useful. 我在另一端收到的是“垃圾”,它看起来很象,我似乎无法将其转换为有用的东西。

Factors I think are influencing it 我认为影响它的因素

  1. 2s complement 2s补码
  2. Potential ASCII or UTF-8 enconding 潜在的ASCII或UTF-8编码

Starting with C code: 从C代码开始:

        // Read 10 bit value into short
        unsigned short ret = decideActionCall(buffer);  
        unsigned char lsb = (unsigned char) ret;
        unsigned char msb = (unsigned char) (ret >> 8);

        printf("msb,lsb = %02x,%02x\n",msb, lsb);

        /**
        char retString [4];
        retString[0] = msb;
        retString[1] = lsb;
        retString[2] = '\n';
        retString[3] = '\0';
        */

        unsigned short retString1[1];
        retString1[0] = ret;

        printf("msb,lsb = %d,%d\n",retString[0],retString[1]);

        printf("String to send: <%s>\n",retString1);
        //if (write(newsockfd, retString, 3) < 0) {
        //  error("Error sending response to the server");
        //}
        if (write(newsockfd, &retString1, 2) < 0) {
          error("Error sending response to the server");
        }
        if (write(newsockfd, "\r",1) < 0) {
          error("Error sending response to the server");
        }

And the terminal output is 终端输出是

START-----------------------------

0 0
136 0x88
ADC3 = 136

END-----------------------------
decideActionCall() ret value: 136
msb,lsb = 00,88
msb,lsb = 0,136
String to send: <�>

So the return for 'ret' is a value between 0 and 1023 (0x3FF). 因此,“ ret”的返回值为0到1023(0x3FF)之间的值。 These 10 bits need to be transferred from C server to client. 这10位需要从C服务器传输到客户端。 I have tried two implementations where I send them as 8 bit chars (retString) or one 16 bit short (retString1). 我尝试了两种实现,它们以8位字符(retString)或一种16位短(retString1)的形式发送。 Sizeof(char) == 1 , and sizeof(short) == 2 . Sizeof(char) == 1sizeof(short) == 2

Java Code (with various attempts to parse it): Java代码(尝试进行多种解析):

BufferedReader bufIn = new BufferedReader(new InputStreamReader(in));
String response = bufIn.readLine();

char [] hello = response.toCharArray();
for (char a : hello) {
    short shortA = (short) (a & 0x3FF);
    System.out.println(a + " = " + (int)a + " = " + shortA);


    //System.out.println((int)a);
    //System.out.println(Integer.valueOf(a+"",10));
    }

byte test[] = response.getBytes("UTF-8");

StringBuilder sb = new StringBuilder();
for (byte b : test) {
     sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString());

byte b05 = 0x00;
byte b06 = 0x00; // MSB, positive as < 0x80

int i = 0;
for (byte a : test) {
    //System.out.println(short1);
    System.out.println("byte: " + a);
    //System.out.println(Integer.valueOf(a+"",10));

    if (i == 0 ) {
        b05 = a;
    } else {
        b06 = a;
    }

    i++;
    }

    System.out.println("i = "  + i);

    byte[] byteTabDay = new byte[2];
    byteTabDay[0] = b05;
    byteTabDay[1] = b06;

    BigInteger temp = new BigInteger(test);
    System.out.println(temp);
    System.out.println(temp.intValue());
    System.out.println(temp.shortValue() + 65536);

    BigInteger temp2 = new BigInteger(byteTabDay);
    System.out.println(temp2);
    System.out.println(temp2.shortValue() + 65536);

Java sample output (android logcat): Java示例输出(android logcat):

I/System.out﹕ response: ���
I/System.out﹕ here: � 65533
I/System.out﹕ ascii: 65485 65581
I/System.out﹕  � = 65533 = 1021
I/System.out﹕ �� = 0 = 0
I/System.out﹕ EF BF BD 00
I/System.out﹕ byte: -17
I/System.out﹕ byte: -65
I/System.out﹕ byte: -67
I/System.out﹕ byte: 0
I/System.out﹕ i = 4
I/System.out﹕ -272646912
I/System.out﹕ -272646912
I/System.out﹕ 48384
I/System.out﹕ -4352
I/System.out﹕ 61184

Questions: 问题:

  1. How should I properly read the socket? 我应该如何正确读取套接字?
  2. How do I properly read the 10 bits? 如何正确读取10位? (I've tried masking the char with 0x3FF) (我尝试用0x3FF掩盖字符)
  3. Is two's complement potentially causing problems? 补码是否可能引起问题?

Thank you, 谢谢,

  1. You're receiving binary, so you shouldn't be using a Reader at all. 您正在接收二进制文件,因此根本不应该使用Reader You should be using DataInputStream.readShort(). 您应该使用DataInputStream.readShort().
  2. That is correct. 那是对的。
  3. No. 没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM