gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:22 24 4
gpt4 key购买 nike

我很难从 C 服务器传递短值。我在另一端收到的看起来像是“垃圾”,而且我似乎无法将其转换成有用的东西。

我认为影响它的因素

  1. 2补数
  2. 可能的 ASCII 或 UTF-8 编码

从 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");
}

终端输出为

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

0 0
136 0x88
ADC3 = 136

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

因此“ret”的返回值介于 0 和 1023 (0x3FF) 之间。这 10 位需要从 C 服务器传输到客户端。我已经尝试了两种实现,我将它们作为 8 位字符 (retString) 或一个 16 位短字符 (retString1) 发送。 Sizeof(char) == 1,和 sizeof(short) == 2

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 示例输出(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

问题:

  1. 我应该如何正确读取套接字?
  2. 如何正确读取 10 位? (我试过用 0x3FF 屏蔽字符)
  3. 二进制补码是否可能导致问题?

谢谢,

最佳答案

  1. 您接收的是二进制文件,所以您根本不应该使用Reader。您应该使用 DataInputStream.readShort()。
  2. 没错。
  3. 没有。

关于java - C 服务器到 Java 客户端套接字读取以短值传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29440791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com