简体   繁体   English

Java-套接字需要延迟才能接收正确的数据吗?

[英]Java - Socket needs delay to receive correct data?

I am writing a Java program using sockets to receive data from a C program. 我正在编写使用套接字从C程序接收数据的Java程序。

The C program mallocs an integer array to fit the number of elements to be put inside. C程序m分配一个整数数组以适合要放入其中的元素数。 For example: {111,2,2,2,3,3} (111 is just a message header), so the int[] size is 6. (We free the array after sending is completed with a return value of 0 which is success). 例如:{111,2,2,2,3,3}(111只是消息头),因此int []的大小为6。(发送完成后,返回值为0,则释放数组。是成功)。 We print out the contents of the array, everything is displayed as expected: 111,2,2,2,3,3 我们打印出数组的内容,所有内容均按预期显示:11、2、2、2、3、3

However, we realize that in Java, we need to add a minor delay before reading from input stream, otherwise we can't get the correct values. 但是,我们意识到在Java中,我们需要在从输入流读取之前添加一个较小的延迟,否则我们将无法获得正确的值。 Eg if we don't put a Thread.sleep(2000) before input stream available, the value Java receive is like 111,0,0,0,3,3 (eg the value 2 is lost) 例如,如果我们没有在输入流可用之前放置Thread.sleep(2000),则Java接收的值将类似于111,0,0,0,3,3(例如,值2丢失)

1) Does it affect Java if I send int array in C and ask Java to read int by int? 1)如果我在C中发送int数组并要求Java通过int读取int,是否会影响Java?

2) Why does the delay make the data accurate? 2)为什么延迟会使数据准确? We try playing around with 1000,1500 but only 2000 gives the most stable result 我们尝试使用1000,1500,但只有2000可以提供最稳定的结果

3) If in SomeAction.class, i put busy wait: 3)如果在SomeAction.class中,我则忙于等待:

while (!pcClient.readMessage());

or 要么

while (!pcClient.readMessage()) {}

it only go the while loop once and just break out? 它只会进入一次while循环,然后爆发? Whereas if I do this below , it works as intended: 而如果我在下面这样做,它将按预期工作:

while (!pcClient.readMessage()) {System.out.print("");}

1) Does it affect Java if I send int array in C and ask Java to read int by int? 1)如果我在C中发送int数组并要求Java通过int读取int,是否会影响Java?

When you ask Java to read it int by int, it basically waits for next 4 bytes available and builds an int value out of these 4 bytes 当您要求Java逐int读取int时,它基本上等待下一个4个字节可用,并从这4个字节中构建一个int

Looks like your Java client starts reading data before your C server starts writing it - that's why a delay of 2 seconds solve your problem 看来您的Java客户端在C服务器开始写入数据之前就开始读取数据-这就是为什么需要2秒的延迟才能解决您的问题

You can simply remove the if(dataIn.available() > 0) { } condition - dataIn.readInt() will block until the data is available on the wire 您可以简单地删除if(dataIn.available() > 0) { }条件dataIn.readInt()将阻塞,直到网络上的数据可用为止

You're allocating and sending the wrong length. 您正在分配和发送错误的长度。 msg_queue is a pointer, and its size is irrelevant to the message length. msg_queue是一个指针,其大小与消息长度无关。 sizeof msg_queue should be sizeof int throughout. 自始至终, sizeof msg_queue应为sizeof int You're sending too much data, so the receiver is getting out of sync. 您发送的数据过多,因此接收器不同步。

Remove the pointless sleep and available() test. 删除无意义的睡眠和available()测试。 The subsequent reads will block for exactly the right length of time, unlike your sleep. 与您的睡眠不同,后续的读取将在正确的时间长度内阻塞。

You don't need to clear the dynamic message befor freeing it. 您无需清除动态消息即可释放它。 You don't even need to allocate the message dynamically, as its size is known at compile time. 您甚至不需要动态分配消息,因为在编译时就知道消息的大小。

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

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