Need Help Reading Data From Inputstream
I have a robot and a GUI application running on a GUI. I have a while loop on the robot side that is constantly sending data to the GUI.
Before i send a value, i send first a value which the GUI will use to determine how many consecutive values it must read afterwards for instance i send something like;
dataout.writeInt(2);
dataout.writeInt(50);
dataout.writeInt(506);
dataout.writeInt(50);
dataout.flush
Here the GUI reads 2 and then under the case 2, it will read the next two integers.
On the GUI side i have i while loop that is in a run() of a thread that is reading from the inputstream continuosly.
Inside the loop on the GUI i have a switch case statement.
Example
while(true){
int val = dataIn.readIn()
switch(val){
case 1:
int color = readInt();
break;
case 2:
int me= readInt();
int you= readInt();
break;
case 3:
int megg = readInt();
int youss = readInt();
int mes = readInt();
int youe = readInt();
break;
}
}
t is not working as i want. This is what i get:
After it reads the first int, i get a series of numbers that it is reading from the inputstream. i don't know where those numbers come from.
I thought that if it cant read the numbers i send, then it must block, but it isn't.
For the example above this is what i g开发者_JAVA技巧et:
2
1761635840
1946182912
1845523456
1761636096
1845523200
1006658048
16274152968
All the numbers after the 2, i don't know where they come from. it doesn't read the numbers after the 2 i send.
I tried to insert some Thread.sleep(1000) but is not working.
What am i doing wrong? Need help
CODE
//This code on the robot
public class ForkliftColorSensorReader implements Runnable{
DataOutputStream outputStream;
ColorSensor colorSensor;
public ForkliftColorSensorReader(ColorSensor colorSensor, DataOutputStream outputStream) {
this.outputStream = outputStream;
this.colorSensor = colorSensor;
}
public void run() {
int code = 1;
while (code == 1){
try {
Thread.sleep(1000);
outputStream.writeInt(10);
outputStream.flush();
outputStream.writeInt(2);
outputStream.flush();
} catch (Exception e) {
}
}
try {
Thread.sleep(1000);
outputStream.writeInt(20);
outputStream.flush();
outputStream.writeInt(4);
outputStream.flush();
} catch (Exception e) {
}
}
}
}
//This code on the GUI
public class Receive implements Runnable{
int num = this.dataIn.readInt();
public void run(){
switch(num){
case 10:
int color = this.dataIn.read();
break;
case 20:
int c = this.dataIn.read();
break;
default;
}
}
}
// I am using NXTConnector from the GUI to make the connection to the robot.
//I then use the DataOutputstream from the connection to read the data
Does the text intentions<b
mean anything to you? You're reading that from your input stream; that's what those numbers correspond to in a certain character set. My guess is you've got a bit of HTML writing to your output stream. Are you sure you're ONLY writing to the DataOutputStream and not also the underlying OutputStream at the same time? Also, is that really what you read code looks like? If so, how is the readInt() method defined?
EDIT: Snippit for working out the above.
int input = 184549376 ;
byte[] bytes = { (byte)(input >> 24), (byte)(input >> 16),
(byte)(input >> 8), (byte)(input) };
System.out.printf("int: %d hex: %08X string: %s",
input, input, new String(bytes));
EDIT #2: In your code, you write with writeInt()
and read with read()
This is exactly the sort of non-symmetry I was talking about. You must use readInt()
to read a field that was written with writeInt()
. InputStream.read()
, what you are using, reads one byte of data and stores it in an int. Not what you want.
精彩评论